首页 > 解决方案 > 我无法从 ComboBox vb6 保存数据

问题描述

我的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

我的界面(表格):

在此处输入图像描述

AñadirButton或 Add用于将数据Button复制到,我留下一个描述性的:UserControlPictureBoxGIF

在此处输入图像描述

代码到 AñadirButton或添加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

问题是我无法保存值,因为出现以下错误: R un-time error'381 ': invalid property array indexwhen i press Guardar Buttonor Save Button

在此处输入图像描述

在这一行:

在此处输入图像描述

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

GuardarButton或 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

那么,有什么好办法吗?谁能帮我解决这个问题?

在此处输入图像描述 在此处输入图像描述

这是与线AddTypeID = 1

标签: sqlsql-servercomboboxvb6

解决方案


您正在为 UserControl 设计、编码和调试 API。此 API 使您可以访问 UserControl 包含的任何内容,无论是控件内容、计算内容还是其他内容。您的所有代码都应包括错误处理和其他防御性编码技术。让你的代码很难失败。

检索 ID 时,您需要添加一些防御代码:

Public Property Get AddTypeID() As Integer
   If cmbAddType.ListIndex >= 0 Then
      AddTypeID = cmbAddType.ItemData(cmbAddType.ListIndex)
   Else
      AddTypeID = -1
   End If
End Property

现在代码不会失败。但是前端逻辑呢?当 ID 为 -1 时会发生什么?同样,这取决于您作为设计师。但也许是这样的:

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

推荐阅读