sql - 我无法从 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
复制到,我留下一个描述性的:UserControl
PictureBox
GIF
代码到 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 index
when i press Guardar Button
or 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
解决方案
您正在为 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
推荐阅读
- python - 在 Alfred 工作流程中使用 Python:在脚本中调用多个用户输入变量
- docker - Docker-for-desktop kubernetes 从私有仓库拉取镜像
- javascript - Vue - 父级上的深层嵌套对象访问图像
- r - 循环或不循环 - 如何让我的小算法以一种好的方式处理所有观察结果?
- c# - iText7 允许用户编辑文本
- python - 错误:命令出错,退出状态为 1:python setup.py egg_info 检查日志以获取完整的命令输出。尝试安装 dotenv 时
- javascript - 单击按钮后如何添加自定义光标,并将其保存到LocalStorage
- javascript - 为什么这个相对路径在本地工作但在容器中失败?
- ios - Swift - 检查 url 资产是否有声音
- go - Google Sheets API BatchUpdate 中每一行的不同背景颜色