Solved

Unique Values in DB Combo

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

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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