Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ADOQuery SELECT ComboBox

Posted on 2003-11-07
4
Medium Priority
?
2,077 Views
Last Modified: 2012-06-27
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
Comment
Question by:fradolcino
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9700043
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
 

Author Comment

by:fradolcino
ID: 9700281
give me some code for this problem please
thanks
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 9700334
...
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
 
LVL 3

Expert Comment

by:huferry
ID: 9700356
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question