Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2123
  • Last Modified:

ADOQuery SELECT ComboBox

I have a ADOQuery with the filed Names who has those names and even more
(John, John, Mary, John, Eve, Mary, John, John, Mary, Eve)
i want to add in a ComboBox all the names but once
ONLY John, Mary, Eve to appear
How can i do this?
I try with SQL.Add('select distinct.....') but don't work
Thanks
0
fradolcino
Asked:
fradolcino
  • 2
1 Solution
 
kretzschmarCommented:
a
select distinct name from atable
should you give unique names

whereas a
select distinct * from atable
give u unique rows, if there are more fields than name are there

meikl ;-)

0
 
fradolcinoAuthor Commented:
give me some code for this problem please
thanks
0
 
kretzschmarCommented:
...
begin
  combobox1.items.clear;
  adoquery1.close;
  adoquery1.sql.text := 'select distinct name from atable order by name';
  adoquery1.open;
  while not adoquery1.eof do
  begin
    combobox1.items.add(adoquery1.fieldbyname('name').asstring);
    adoquery1.next;
  end;
  adoquery1.close;
end;
...


or, if distinct will not work for you

...
begin
  combobox1.items.clear;
  adoquery1.close;
  adoquery1.sql.text := 'select * from atable order by name';
  adoquery1.open;
  while not adoquery1.eof do
  begin
    if combobox1.items.indexof(adoquery1.fieldbyname('name').asstring = -1 then
      combobox1.items.add(adoquery1.fieldbyname('name').asstring);
    adoquery1.next;
  end;
  adoquery1.close;
end;
...

meikl ;-)
0
 
huferryCommented:
A way to do this, to select the names grouped in your SQL statement.
I don't know whether you'll use the query elsewhere, but you can also
use another ADOQuery component so that this would not disturb your
existing ADOQuery component.

You can use such SQL statement:

select MyTable.Name
from MyTable
group by MyTable.Name

another way to do this, is to filter the names before you add it to your
combo box:

// --- you have done your query at this point.
with ADOQuery do
begin
  ComboBox.Items.Clear;
  First;
  while not Eof do
  begin
    if ComboBox.Items.IndexOf( FieldValues['Name'] ) = -1 then
       ComboBox.Items.Add(FieldValues['Name'];
    Next;
  end;
end;

Explanation:
  ComboBox.Items is a TStrings which has the method called IndexOf. This will return
  the index of a given string. If the string is not listed in the list, it will return -1. The above
  program fragment will add the string (the name) to the combo box item list if it is not
  yet listed.

regards,
huferry

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now