2014-02-16 15 views
0

我需要使用組合框ID來創建一個SQL插入,而我的組合只顯示錶的字段「名稱」,並且我需要該字段「ID」,使插入,就像上了selectedValue VB.NET當組合框顯示另一個字段時,VB6獲得組合框索引以進行插入

順便說一句,這裏是我的代碼(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

我將宣佈一個並行採集保存的值的索引,並由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 

請記住在清除組合框上的項目的同時清除集合!

相關問題