ComboBox update ComboBox

flex24appeal

New member
Joined
Oct 2, 2006
Messages
3
Programming Experience
Beginner
I know that this topic has been covered before but I really need your help on my project. I have three combo boxes; make, model, and year. I want it when I choose the make, I want the models for that make to appear in model combo box. Same as the year.

Im running MySql as my database. The table is called auto and columns are: Auto_ID, Auto_Make, Auto_Model, Auto_Year.

I tried doing the OdbcAdapter. It showed that values but once I choose one it automatically chooses the model as well. Also, it shows all the models even though it is not for that make. For example, if i chose Honda as make, in the model combobox will have accord, odyssy, sentra, altima, etc. Sentra and Altima are not Honda's models. That's where im having problem with.

Please help me. Thank you in advance.
 
That's an inappropriate design. You should have a Make table that has an ID column and a Name column. You should also have a Model table that has an ID column, a MakeID column and a Name column. You would have an Auto table that has an ID column, a ModelID column and a Year column. You create realtionships between the tables and that's how you navigate between them. The MakeID column in the Model table is a foreign key from the ID column in the Make table, which means that MakeID must be equal to a value from the ID column of the Make table. The ModelID column of the Auto table is also a foreign key from the ID column of the Model table. Now, when you select a record from the Auto table you use the ModelID to get the corresponding Model record, then that Model's MakeID to get the corresponding Make record.

Once you've done that you can then bind your ComboBoxes to different data sources and the data-binding mechanism will update the contents of each control automatically. You fill your three tables in a DataSet and create the two DataRelations between the tables. You then bind the first ComboBox to the Make table, the second ComboBox to the relationship between the Make and Model tables and the third ComboBox to the relationship between the Model and Auto tables. Everything else will be done for you.
 
You can fake the structure that Mr McIlhinney describes, if your table is already in place and you are unwilling to split it. I further do not recommend calling your table auto, as it is used in some sql syntaxes as a keyword.

Have 3 DataTables formed from these queries:

SELECT auto_make FROM auto GROUP BY auto_make
SELECT auto_make, auto_model FROM auto GROUP BY auto_make, auto_model
SELECT auto_model, auto_year FROM auto GROUP BY auto_model, auto_year

Generating these datatables will allow you to strike relationships between them. For lower resource usage, I recommend you do not fill successive tables until the user has selected an item in the current box, though it is perfectly possible to completely fill each table and have the relationships do the work

It is likely you will have to write any update logic yourself.

Additionally, i cant see the point of auto_id - the other 3 columns (by implicity) seem to make up a primary key.
 
Back
Top