Solved

Unique Values in DB Combo

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

19 Experts available now in Live!

Get 1:1 Help Now