Reputation: 1224
I'd need some help since I don't see any solution after doing some search...
I have a database Database.mdb, which have a few tables, I'm reading specific table Fixtures, which have fields ID, ChNo, Default and Manufacturer;
I then have a form on which I'd like a user to be able to choose from each fixture, but would like to sort it by manufacturer firstly. So, I added ListBox component on a form, and started to populate it using the code provided:
procedure TPatchNew1.FormShow(Sender: TObject);
var No, No2: integer;
begin
No2:=0;
AdoQuerySearchFixtures.SQL.Text:='SELECT Manufacturer FROM Fixtures';
AdoQuerySearchFixtures.Open;
ManuNo:=AdoQuerySearchFixtures.RecordCount;
While No2 < No do
begin
ManufacturerListbox.Items.Add;
AdoQuerySearchFixtures.Close;
AdoQuerySearchFixtures.SQL.Clear;
AdoQuerySearchFixtures.SQL.Text:='SELECT Manufacturer FROM Fixtures WHERE ID='+IntToStr(No2+1);
AdoQuerySearchFixtures.Open;
ManufacturerListbox.Items[No2].Caption:=AdoQuerySearchFixtures.Fields[0].AsString;
No2:=No2+1;
end;
end;
This does the trick, but I'd like it to first: merge all duplicates, so that the fixtures with the same Manufacturer field value are all shown only after the Listbox item is clicked (I believe I'll be able to do that changing SQL text to "WHERE Manufacturer=something"), but there are not 100 items in listbox, if I have 90 fixtures from same manufacturer; At last, before the listbox is populated, I'd like it to be sorted alphabetically, from A to Z. Should I firstly read all manufacturers to sort of a record or array, then find duplicates and delete them, and later sort it, lastly filling in to listbox items, or is there any other way? It that's the way, how to do it? I give up after this whole day.. :(
Thanks.
Cheers
Upvotes: 0
Views: 2185
Reputation: 6477
If I understand you correctly, you want to display in a listbox the names of manufacturers who supply fixtures. It's not clear from your question, but I am assuming that you have at least two tables: one of manufacturers and one of fixtures. The manufacturer will be a foreign key in the fixtures table.
If you want to load the listbox with the manufacturers, then it is better to take the data from the manufacturers table as you are (almost) guaranteed to have no duplicates there. Your sql query would be
select id, name
from manufacturers
order by name
If you want only manufacturers who appear in the fixtures table, then alter the query thus
select id, name
from manufacturers
where exists (select 1 from fixtures
where fixtures.manufacturer = manufacturers.id)
order by name
Load the listbox in the following manner
manulist.items.clear;
with qManufacturers do
begin
open;
while not eof do
begin
manulist.items.addobject (fieldbyname ('name').asstring,
tobject (fieldbyname ('id').asinteger));
next
end;
close
end;
Then when a name is chosen from the listbox, you can display the fixtures thus
with manulist do
begin
manu:= longint (Items.Objects[itemindex]); // manufacturer key
mname:= items[itemindex]; // manufacturer name
end;
with qFixtures do begin params[0].asinteger:= manu; open; .... end;
qFixtures would be something like
select id, name, ...
from fixtures
where manufacturer = :p1
Upvotes: 1