简体   繁体   English

我无法从 ComboBox vb6 保存数据

[英]I can't save data from ComboBox vb6

My UserControl :我的UserControl

在此处输入图片说明

All code from UserControl :来自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 : Añadir Button或 Add Button用于将UserControl数据复制到PictureBox ,我留下一个描述性的GIF

在此处输入图片说明

Code to Añadir Button or Add Button :代码到 Añadir Button或添加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: R un-time error'381 ': invalid property array index when i press Guardar Button or Save Button .问题是我无法保存值,因为出现以下错误: R un-time error'381 ': invalid property array index when I press Guardar Button或 Save Button

在此处输入图片说明

In this line:在这一行:

在此处输入图片说明

AddTypeID = cmbAddExample(indice).ItemData(cmbAddExample(indice).ListIndex)

Code to Guardar Button or Save Button : Guardar Button或保存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这是行AddTypeID = 1

You are designing, coding, and debugging an API for the UserControl.您正在为 UserControl 设计、编码和调试 API。 This API gives you access to whatever the UserControl contains whether the contents of a control, a calculation or anything else.此 API 使您可以访问 UserControl 包含的任何内容,无论是控件的内容、计算还是其他任何内容。 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:在检索 ID 时,您需要添加一些防御性代码:

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?当 ID 为 -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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM