Solved

ADOQuery SELECT ComboBox

Posted on 2003-11-07
4
1,846 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
  • 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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now