Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dependent combo boxes

Status
Not open for further replies.

henslecd

MIS
Apr 21, 2003
259
0
0
US
How do you create dependent combo boxes?

I.e. Choose car from first dropdown. Then in second dropdown it gives you a list of cars. Once you choose a car, the third dropdown gives you options that only that car has etc etc.

 
This should give you an idea just drop this onto a new form dont change the name from form1 and put 3 comboboxes on the form. Its really basic but should show you how to do it.

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

ComboBox1.Items.Add("Toyota")
ComboBox1.Items.Add("Ford")
ComboBox1.Items.Add("Nissan")

End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Select Case ComboBox1.Text
Case Is = "Toyota"
ComboBox2.Items.Add("Tundra")
ComboBox2.Items.Add("Corolla")
ComboBox2.Items.Add("Camry")
Case Is = "Ford"
ComboBox2.Items.Add("F150")
ComboBox2.Items.Add("Mustang")
ComboBox2.Items.Add("MustangGT")
Case Is = "Nissan"
ComboBox2.Items.Add("PathFinder")
ComboBox2.Items.Add("B2200")
ComboBox2.Items.Add("Other Nissan Car")
End Select

End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

Select Case ComboBox3.Text
Case Is = "Tundra"
ComboBox3.Items.Add("PowerLocks")
ComboBox3.Items.Add("Sun Roof")
ComboBox3.Items.Add("50 Cal Machine Gun")
Case Is = "Corrola"
ComboBox3.Items.Add("Wheels w/ spikes")
ComboBox3.Items.Add("Oil Slicks")
ComboBox3.Items.Add("20000 Year Warranty")
Case Is = "Camry"
ComboBox3.Items.Add("Alarm")
ComboBox3.Items.Add("Gold Trim Package")
ComboBox3.Items.Add("A/C")
Case Is = "F150"
ComboBox3.Items.Add("Tires")
ComboBox3.Items.Add("A Motor")
ComboBox3.Items.Add("Windows")
Case Is = "Mustang"
ComboBox3.Items.Add("Doors")
ComboBox3.Items.Add("Roll Cage.. but why???")
ComboBox3.Items.Add("Muddin Tires")
Case Is = "MustangGT"
ComboBox3.Items.Add("Low Pros")
ComboBox3.Items.Add("Spokes")
ComboBox3.Items.Add("Ubah System")
Case Is = "PathFinder"
ComboBox3.Items.Add("M&M's")
ComboBox3.Items.Add("Floor Mats")
ComboBox3.Items.Add("A Rear view mirror")
Case Is = "B2200"
ComboBox3.Items.Add("Seats")
ComboBox3.Items.Add("Air Bags")
ComboBox3.Items.Add("Side Air Bags")
Case Is = "Other Nissan Car"
ComboBox3.Items.Add("BackSeat Airbags")
ComboBox3.Items.Add("Roof Airbags")
ComboBox3.Items.Add("Airbags for the Airbags")
End Select

End Sub

Hope it helps, Matt
 
Sorry one thing that last select case statement ComboBox2_SelectedIndexChanged the Select Case Combobox3 should be changed to ComboBox2.

Sorry Matt
 
I actually want the values to be pulled from my DB. I have thousands of values so I can't write them in statically. Do you know how to do that?
 
Well in that case you would first need to return a recordset of your first level choices. Then in combobox1.selectedindex change you would return another recordset based on the parameter you choose in combobox1.text. Then you would populate combobox2 like

Combobox1.selectedindexchanged

GetMyNewRecordSet2ndLayer passing combobox1.text as Parameter

Dim x as int16
For x = 0 to myNewRecordset.tables(0).rows.count - 1

combobox2.items.add(mynewrecordset.tables(0).rows(x).items("TheValueField")

Next

And then the this will basically do the same thing for the next combobox, just passing combobox2.text as your parameter, and then loading the items into the combobox3.

-Matt
 
OK I am going to try that. Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top