OneCompiler

How do i populate say combobox2 depending on a *selected* ms access database table in combobox1 without writing If statements for each and every table in the database in vb.net windows application form

I would want to prompt user to select a tool name from combobox1, then out automatically, combobox2 gets filled with tool description and size , which is a field in the selected tool name table. Currently i have been using the following code, however the problem comes when i would like to update the database and add another table. This would mean adding the code in the application as well. I would like the application to be able to detect that a new tool table has been added and show that automatically without having to code again

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

' ADDING TABLE NAMES TO COMBOBOX 1 (TOOL NAME)

con.Open()

Dim schemaTable As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})

For Each dr As DataRow In schemaTable.Rows

        Combobox1.Items.Add(dr.Item("TABLE_NAME"))

Next

End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Combobox1.SelectedIndexChanged

'FILLING COMBOBOX 2 (TOOL DESCRIPTION AND SIZE) DEPENDING ON SELECTION IN COMBOBOX 1 (TOOL NAME)

If Combobox1.SelectedIndex = 1 Then

Combobox2.Text = ""

con.Open()

Combobox2.Items.Clear()

Dim dr As OleDbDataReader

Dim cmd As New OleDbCommand

cmd.CommandText = "Select * from 14_POUND_HAMMER"

cmd.Connection = con

dr = cmd.ExecuteReader

While dr.Read

Combobox2.Items.Add(dr.GetString(1))

End While

dr.Close()

con.Close()

ElseIf Combobox1.SelectedIndex = 2 Then

Combobox2.Text = ""

con.Open()

Combobox2.Items.Clear()

Dim dr As OleDbDataReader

Dim cmd As New OleDbCommand

cmd.CommandText = "Select * from 3_QUARTER_IMPACT_WRENCH" cmd.Connection = con

dr = cmd.ExecuteReader

While dr.Read

Combobox2.Items.Add(dr.GetString(0))

End While

dr.Close()

con.Close()

ElseIf Combobox1.SelectedIndex = 3 Then

Combobox2.Text = ""

con.Open()

Combobox2.Items.Clear()

Dim dr As OleDbDataReader

Dim cmd As New OleDbCommand

cmd.CommandText = "Select * from ABRASIVE_CUT_OFF_SAW " cmd.Connection = con

dr = cmd.ExecuteReader

While dr.Read

Combobox2.Items.Add(dr.GetString(0))

End While

dr.Close()

con.Close()

No answers yet!

2 years ago by