Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unique Values in DB Combo

Posted on 1997-09-14
5
Medium Priority
?
399 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
[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
  • 2
5 Comments
 

Accepted Solution

by:
rainbowsoftware earned 100 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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