Solved

Unique Values in DB Combo

Posted on 1997-09-14
5
390 Views
Last Modified: 2010-04-04
I have a DB form with a drop down DBField called 'City' .  When a user is adding/editing a record, they can select values from the City drop down box on the form.  This drop down list should only contain unique values from data already in the table.  How do I...

1      When the form is created, I want to search/query the table for unique instances of Cities in the City field.  For example, if the table contains 1,000 records and 500 have 'Atlanta' in the City field and the other 500 have 'Chicago', I only want Atlanta and Chicago to appear as values in the form's City drop down box ONCE.  This is sort of like A lookup DBField but that would display Chicago and Atlanta 500 times each.  

2      Once I find the unique values, I want to add them to the City DB Dropdown box.  I know once I get the list of values I want to be in the list, I should use  the ADD method to the TStrings of the combobox, but how do I get the query/search (or whatever) results into the strings?

0
Comment
Question by:d4jaj1
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
rainbowsoftware earned 50 total points
ID: 1344860
You can use a Query component and add the SQL string SELECT DISTINCT CITY FROM DBDEMOS
Then your table will contain the citynames only once.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1344861
That's a good answer, but it isn't the answer to the question I asked.  I asked to questions, 1) how to filter the form's drop down Combobox values - not the table and 2) once I find the values, how do I add them to the string list.  Do you have an answer for these questions?
0
 

Expert Comment

by:rainbowsoftware
ID: 1344862
As I understand your question, you want unique cities in a DBComboBox and that is what you get with the SQL string. But obviously I don't understand your question, so I am sorry I can't help more.
0
 

Expert Comment

by:joseramos
ID: 1344863
I think the answer from rainbowsoftware was correct. However I think what you are looking for is something like this:

AQuery: TQuery;
{ ..... I am assuming your TQuery has been properly setup }

with AQuery do
begin
     try
         Active := False
         SQL.Clear;
         SQL.Add('Select distinct city from dbdemos');

         try
             Active := True;
         except
             on E: Exception do
                    raise;   { or handle it however yoy want }
         end;

         AComboBox.Clear;
         First;

         while not EOF do
         begin
              AComboBox.Items.Add(FieldByName('city').AsString);
              next;
         end;

     finally
         Active := False;
     end;
end;

This code could be put in your FormCreate or FormShow
handlers. The Query suggested by rainbowsoftware guarantees
no duplicate values are retrieved. The rest of the code inserts
the strings in the combobox (no duplicate values will be found,
guaranteed !!!).

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1344864
The comment provided the correct answer.  Thanks jose.  I graded this a C because rainbow... didn't give the correct answer, but did attempt to.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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