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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate multiple combo boxes with same info 1

Status
Not open for further replies.

tubbypascal

Programmer
May 31, 2006
3
CA
Hello,
In a Web Application I have to populate 7 combo boxes with the same info. To populate one box I use the following code:

"If IWComboBoxStateProv.ItemIndex = 0 then
begin
IWComboBoxCity1.Items.Insert(0,'Airdrie');
IWComboBoxCity1.Items.Insert(1,'Brooks');
IWComboBoxCity1.Items.Insert(2,'Calgary');
etc...
end
else If IWComboBoxStateProv.ItemIndex = 1 then
begin
IWComboBoxCity1.Items.Insert(0,'Abbotsford');
IWComboBoxCity1.Items.Insert(1,'Armstrong');
etc...
end
else if... etc...etc...."

Some States and Provinces have a LOT of cities. Multiply this by IWComboBoxCity2, IWcomboBoxCity3 etc.. it becomes a rather large amount of coding. Does anyone know or have any ideas as to how I can make this process shorter or more efficient ?
Thanks :)
 
Build a table that contains records identifing which cities are in each province and once IWComboBoxStateProv is selected run a query that gets all the cities in that province, loop through the recordset and add them to the Items.
[tt]
Table: Province_Cities
ID Province City
1 Alberta Airdrie
2 Alberta Brooks
3 Alberta Calgary
4 BC Abbotsford
5 BC Armstrong
[/tt]

In the OnSelect Event of IWComboBoxStateProv do something like:

[tt]
with qryGetCities do
begin
SQL.Clear;
SQL.Add('SELECT * FROM Province_Cities WHERE Province = ' + QuotedStr(IWComboBoxStateProv.Text));
Active := true;
While not eof do
begin
IWComboBoxCity1.Items.Insert(0, FieldByname('CITY').AsSTring);
NExt;
end;
end;
[/tt]

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases: The Fundamentals of Relational Database Design
 
Thanks guys. I decided to go with Lespaul's solution, as it seemed to fit my app the best. Don't know why I didn't think of it myself, I use this same stuff all through my app.. ..duh. Anyway, this is the code I used, it works fast and well....thanks again :)

with UserSession.QueryStateProv do
begin
Active := False;
SQL.Clear;
SQL.Add('SELECT * FROM StateProvCities');
SQL.Add('WHERE fldStateProv =:STATEPROV');
ParamByName('STATEPROV').AsString := StateProv;
Active := True;
While not eof do
begin
IWComboBoxCity.Items.Insert(0,FieldByName('fldCity').AsString);
IWComboBoxCity1.Items.Insert(0,FieldByName('fldCity').AsString);
IWComboBoxCity2.Items.Insert(0,FieldByName('fldCity').AsString);
IWComboBoxCity3.Items.Insert(0,FieldByName('fldCity').AsString);
IWComboBoxCity4.Items.Insert(0,FieldByName('fldCity').AsString);
IWComboBoxCity5.Items.Insert(0,FieldByName('fldCity').AsString);
IWComboBoxCity6.Items.Insert(0,FieldByName('fldCity').AsString);
Next;
end; //while not
end; //with QueryStateProv
UserSession.CDSetStateProv.Open;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top