Solved

Unique Values in DB Combo

Posted on 1997-09-14
5
388 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to implement a User Shortcut mapper? 1 95
JAudiorecorder record freezing the app 29 60
Base1 Encode/Decode 3 68
How to build JSON File in Delphi 6 3 14
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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

12 Experts available now in Live!

Get 1:1 Help Now