我无法从 ComboBox vb6 保存数据
I can't save data from 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ñadir Button
或 Add Button
用于将 UserControl
数据复制到 PictureBox
,我留下描述 GIF
:
代码到 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
问题是我无法保存这些值,因为出现以下错误:当我按 Guardar Button
或保存 Button
时出现 Run-time error'381 ': invalid property array index
。
这一行:
AddTypeID = cmbAddExample(indice).ItemData(cmbAddExample(indice).ListIndex)
代码到 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
那么,有什么建议吗?谁能帮我解决这个问题?
这是 AddTypeID = 1
行
您正在为 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
我的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ñadir Button
或 Add Button
用于将 UserControl
数据复制到 PictureBox
,我留下描述 GIF
:
代码到 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
问题是我无法保存这些值,因为出现以下错误:当我按 Guardar Button
或保存 Button
时出现 Run-time error'381 ': invalid property array index
。
这一行:
AddTypeID = cmbAddExample(indice).ItemData(cmbAddExample(indice).ListIndex)
代码到 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
那么,有什么建议吗?谁能帮我解决这个问题?
这是 AddTypeID = 1
您正在为 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