Home » questions » I want to use a combobox or datacombo in visual Basic.?

I want to use a combobox or datacombo in visual Basic.?

2006-08-01 17:39:17, Category: Programming & Design
I want to pull data from a MS Access database y show then in 2 columns using either a combobox o datacombo and use the first column to update a field in another table.

Answers

  1. cmr

    On 2006-08-01 22:40:37


    I have not understood your question exactly. But I think that you want to display in the combobox or DCombo box two fields of a table . I am submitting the code for displaying two fields in a combo box, it is quite lengthy. See if you can make it shorter. In case this is not the one you want. Please ask the question as precise as possible 'This code is for opening the database and populating the combo box 'This is usually included in Form load event ' The below is related to customer combo box Dim stComboText As String On Error GoTo Error With MyCon .Provider = "Microsoft.jet.oledb.4.0" .ConnectionString = "data source=c:\r.mdb" .Open End With With cmd Set .ActiveConnection = MyCon .CommandType = adCmdTable .CommandText = "Bill_Details" End With With Rs .LockType = adLockPessimistic .CursorType = adOpenKeyset .Open cmd End With If Rs.RecordCount > 0 Then Do Until Rs.EOF ' If you want to include only one of the fields e.g orderno. only then exclude the other fields stComboText = Rs("orderno") & "," & Rs("customer_id") Combo1.AddItem stComboText Rs.MoveNext Loop Rs.Close Else MsgBox "No Records Found to fill ComboBox...", vbInformation, "No Records" Rs.Close End If On Error GoTo 0 Form_Load_Exit: Exit Sub Error: MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error in [Form_Load]" End 'In the command click event Private Sub Combo1_Click() Dim istSearchString As Integer Dim iStringLength As Integer Dim stQuery As String Dim iPos As Integer Dim stComboText As String On Error GoTo Error 'This will find out the position of "," character iPos = InStr(Combo1.Text, ",") 'This will give the length of the string iStringLength = Len(Combo1.Text) 'This will return the value 'eg. The combobox displays the orderno and customer_id as 1001,101 'then the value of ipos=5, the value of iStringLength=8 'the istSearchString will return the value 1001 istSearchString = Val(Mid$(Combo1.Text, 1, iPos)) stQuery = "SELECT * " & _ "FROM bill_details " & _ "WHERE orderno = " & istSearchString With cmd Set .ActiveConnection = MyCon .CommandType = adCmdText .CommandText = stQuery End With With Rs .LockType = adLockPessimistic .CursorType = adOpenKeyset .Open cmd End With If Rs.RecordCount > 0 Then ' You can display whatever fields you want. Here for convience I have chosen one ' The field that has been displayed in the textbox can be manipulated Text1.Text = Rs!customer_name Rs.Close Else MsgBox "No Match Found", vbInformation, "No Match" Rs.Close End If On Error GoTo 0 Combo1_Exit: Exit Sub Error: MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error in [Combo1_Click]" End Sub