Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
I am just developing a project in VB.Net and Access 2007 and i just got stuck at a point where I just need to Populate a Listbox from Access 2007 database but if I change an item from the combo box the listbox should change accordingly. I have the code to populate the listbox but items are not changing after changing combo box items.
Please guide me in this regard.

Private Sub frmneworder_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cmbmain.Text = "Select Main Item"
        Call loadinvestigations()
    End Sub

Sub loadinvestigations()
        Con.Open()
        Dim cmd As New OleDbCommand("select * from items", Con)
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        While dr.Read
            lstitem.Items.Add(dr(1).ToString.ToUpper)
        End While
        dr.Close()
        Con.Close()



Private Sub cmbmain_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbmain.SelectedIndexChanged
       Con.Open()
       Dim cmd As New OleDbCommand("select item_name from items where cat_name='" & txtopd.Text & "'", Con)
       Dim dr As OleDbDataReader = cmd.ExecuteReader()
       While dr.Read
           lstitem.Items.Add(dr(1).ToString.ToUpper)
       End While
       dr.Close()
       Con.Close()
   End Sub
Posted
Comments
Maciej Los 10-Dec-14 13:34pm    
Replace:
Dim cmd As New OleDbCommand("select item_name from items where cat_name='" & txtopd.Text & "'", Con)
with:
Dim cmd As New OleDbCommand("select item_name from items where cat_name='" & , cmbmain.SelectedValue & "'", Con)

1 solution

Hello guys It worked finally with the following code:


VB
Private Sub cmbmain_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbmain.SelectedValueChanged
        lstitem.Items.Clear()
        Con.Open()
        Dim cmd As New OleDbCommand("select * from items where cat_name='" & cmbmain.Text & "'", Con)
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        While dr.Read
            lstitem.Items.Add(dr(1).ToString.ToUpper)
        End While
        dr.Close()
        Con.Close()
    End Sub


I thank Mt Maciej Los for his kind help.
Thank you
 
Share this answer
 
Comments
Maciej Los 13-Dec-14 8:50am    
You're very welcome ;)

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