Solved

ADOQuery SELECT ComboBox

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

759 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