Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I form in access database file, in this form have two combobox, these two combobox bind data from same table.
How can I write code in order to make user when select item from the first combobox the second combobox will automatically select an item based on the index of the item selected from combobox?

thanx
Posted
Comments
Maciej Los 19-Jul-13 15:49pm    
VB or VBA?
What have you done so far? Where are you stuck?
Rasool Ahmed 20-Jul-13 3:43am    
VBA, I didn't start yet.

Try something like this:
VB
Private Sub ComboBox1_Change()
    Dim sSQL AS String

    sSQL = "SELECT FieldName1" & vbcr & _
        "FROM TableName" & vbcr & _
        "WHERE FieldName1 = " & Me.ComboBox1.Value

        Me.ComboBox2.RowSource = sSQL
End Sub
 
Share this answer
 
Thank you Maciej Los,
I improved your answer to this:
when choose ItemId combobox, NCode combobox will choose the correct item according to ItemId value, and vice versa.
VB
Private Sub ItemId_Change()
Dim sSQL As String
    sSQL = "SELECT Items.NationalCode, Items.ItemId " & "FROM Items" & " WHERE Items.ItemId = " & Me.ItemId.Value
        Me.NCode.RowSource = sSQL
        Me.NCode = Me.NCode.ItemData(0)
End Sub

Private Sub ItemId_GotFocus()
Dim sSQL As String
    sSQL = "SELECT Items.GenericName, Items.ItemId" & " FROM Items"
        Me.ItemId.RowSource = sSQL
End Sub

Private Sub NCode_Change()
Dim sSQL As String
    sSQL = "SELECT Items.GenericName, Items.ItemId" & " FROM Items" & " WHERE Items.ItemId = " & Me.NCode.Value
        Me.ItemId.RowSource = sSQL
        Me.ItemId = Me.ItemId.ItemData(0)
End Sub

Private Sub NCode_GotFocus()
Dim sSQL As String
    sSQL = "SELECT Items.NationalCode, Items.ItemId" & " FROM Items"
        Me.NCode.RowSource = sSQL
End Sub
 
Share this answer
 
Comments
Maciej Los 25-Jul-13 5:56am    
You're welcome ;)
Please mark both answers as "solved" (green button) to remove your question from "Unanswered Questions" list.
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900