My UserControl
:
All code from UserControl
:
Option Explicit Dim cnn As Connection Dim indice As Integer Public Property Get AddTypeID() As Integer AddTypeID = cmbAddExample(indice).ItemData(cmbAddExample(indice).ListIndex) End Property Public Property Let AddTypeID(ByVal Value As Integer) cmbAddExample(indice).ItemData(cmbAddExample(indice).ListIndex) = Value End Property Public Property Get AddType() As String AddType = cmbAddExample(indice).Text End Property Public Property Let AddType(ByVal Value As String) cmbAddExample(indice).Text = Value End Property Public Property Get AddNumber() As String AddNumber = Text1(indice).Text End Property Public Property Let AddNumber(ByVal Value As String) Text1(indice).Text = Value End Property Public Sub CargarComboUno(ByVal Data As ADODB.Recordset) cmbAddExample(indice).Clear Data.Open "SELECT idTipo, tipo FROM tipo_Numero", cnn, adOpenDynamic, adLockOptimistic Do While Not Data.EOF cmbAddExample(indice).AddItem Data!tipo cmbAddExample(indice).ItemData(cmbAddExample(indice).NewIndex) = Data!idTipo Data.MoveNext Loop End Sub Private Sub IniciarConexion() Set cnn = New ADODB.Connection With cnn .CursorLocation = adUseClient .Open "PROVIDER=MSDASQL;driver={SQL Server};server=database;uid=database;pwd=database;database=database;" End With End Sub Private Sub UserControl_Initialize() Call IniciarConexion End Sub
My interface (form):
The Añadir Button
or Add Button
serves to copy the UserControl
data to the PictureBox
, I leave a descriptive GIF
:
Code to Añadir Button
or Add Button
:
Private Sub btnAñadir_Click() Set rs = New Recordset rs.CursorLocation = adUseServer indice = indice + 1 Load uc1(indice) Set uc1(indice).Container = Picture1 uc1(indice).Visible = True uc1(indice).Top = IIf(indice = 1, 0, uc1(indice - 1).Top + uc1(indice - 1).Height + 20) uc1(indice).CargarComboUno rs uc1(indice).AddNumber = uc1(0).AddNumber uc1(0).AddNumber = "" uc1(indice).AddType = uc1(0).AddType uc1(0).AddType = "" Picture1.Visible = True If indice = 3 Then Me.btnAñadir.Enabled = False End If End Sub
The problem is that I cannot save the values because the following error comes up: Run-time error'381 ': invalid property array index
when i press Guardar Button
or Save Button
.
In this line:
AddTypeID = cmbAddExample(indice).ItemData(cmbAddExample(indice).ListIndex)
Code to Guardar Button
or Save Button
:
Private Sub btnGuardar_Click() Dim i As Integer Dim id As String Dim sel As Integer Call IniciarConexion Dim CM As ADODB.Command For i = 0 To indice id = uc1(i).AddTypeID sel = uc1(i).AddNumber Set CM = New ADODB.Command Set CM.ActiveConnection = cnn CM.CommandType = adCmdText CM.CommandText = "INSERT INTO ejemplodOS(combo,nombre) VALUES (?,?)" CM.Parameters.Append CM.CreateParameter("@cmb", adInteger, , , id) CM.Parameters.Append CM.CreateParameter("@txt", adInteger, , , sel) CM.Execute , , adExecuteNoRecords Next End Sub
So, any sugerence? can anyone help me to solve this problem?
This is with the line AddTypeID = 1
Advertisement
Answer
You are designing, coding, and debugging an API for the UserControl. This API gives you access to whatever the UserControl contains whether the contents of a control, a calculation or anything else. All your code should include error handling and other defensive coding techniques. Make it hard for your code to fail.
When retrieving the ID you need to add some defensive code:
Public Property Get AddTypeID() As Integer If cmbAddType.ListIndex >= 0 Then AddTypeID = cmbAddType.ItemData(cmbAddType.ListIndex) Else AddTypeID = -1 End If End Property
Now the code will not fail. But how about the front-end logic? What should happen when the ID is -1? Again, this is up to you as the designer. But perhaps something like this:
Private Sub btnGuardar_Click() Dim i As Integer Dim id As Integer Dim sel As String Dim CM As ADODB.Command For i = 0 To indice id = uc1(i).AddTypeID sel = uc1(i).AddType If id > 0 Then Set CM = New ADODB.Command Set CM.ActiveConnection = cnn CM.CommandType = adCmdText CM.CommandText = "INSERT INTO ejemplodOS(combo,nombre) VALUES (?,?)" CM.Parameters.Append CM.CreateParameter("@cmb", adInteger, , , id) CM.Parameters.Append CM.CreateParameter("@txt", adInteger, , , sel) CM.Execute , , adExecuteNoRecords Else MsgBox "Respond as you want for an invalid id" End If Next End Sub