Solved

ADOQuery SELECT ComboBox

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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