我需要使用组合框ID来制作一个sql插件,而我的组合只显示表的字段“Name”,我有点需要字段“Id”来插入,就像VB.NET上的selectedValue一样
这是我的代码(VB6)对组合框选择的调用
Public Sub chamaCombo()
Dim con As New ADODB.Connection
Dim rsCombo As New ADODB.recordset
Dim conString As String
constring="Provider=SQLNCLI10;Server=MySv;Database=MyDb;Trusted_Connection=yes"
con.Open conString
rsCombo.Open "Select * from tbdClient", con, adOpenDynamic
Do While rsCombo.EOF <> True
cmb_client.AddItem rsCombo("Name").Value
cmb_client.ItemData(cmb_client.NewIndex) = rsCombo("IdClient").Value
txt_idclient.Text = rsCombo("IdClient").Value 'trying to pass to a txt but its no use
rsCombo.MoveNext
Loop
End Sub
答案 0 :(得分:0)
我会声明一个并行集合,它包含索引的值并由SQL ID键入。您的代码可能如下所示:
Private mCol As New Collection
Public Sub chamaCombo()
Dim con As New ADODB.Connection
Dim rsCombo As New ADODB.recordset
Dim conString As String
constring="Provider=SQLNCLI10;Server=MySv;Database=MyDb;Trusted_Connection=yes"
con.Open conString
rsCombo.Open "Select * from tbdClient", con, adOpenDynamic
Do While rsCombo.EOF <> True
cmb_client.AddItem rsCombo("Name").Value
mCol.Add("K_" & CStr(cmb_client.NewIndex), rsCombo("IdClient").Value)
rsCombo.MoveNext
Loop
End Sub
Private cmb_client_Click()
Dim ID As String
' Don't forget to test the ListIndex for a invalid value (ex: -1)
ID = mCol("K_" & CStr(cmb_client.ListIndex))
MsgBox ID
End Sub
请记住在清除组合框中的项目的同时清除集合!