INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

populating a DBCombobox

populating a DBCombobox

(OP)
I have 2 databases with a master/detail relationship. Let's call the master (Project) and the detail (Trial)
I would like to have 2 DBCombobox that are populated with the name of each project in the first DBcombobox and then its associated trial in the second DBCombobox.
also when i select a different project, I would like to have the trial dbcombobox to be refreshed.

The project database looks like that:
Project_ID (Keyfield)
Project_Name

The trial Database looks like that
Trial_D (keyfield)
Project_ID
Trial)Name.

I do not know if that can be accomplished or is there a better way to handle that. Right now i am using 2 DBgrid but I do not like the look of it. Too bulky.

Thanks.
P

RE: populating a DBCombobox

I believe you can make the connection of datasets of Projects and Trials through the properties MasterSource / MasterFields, then use their DataSources as ListSources of DBLookupComboBox's.

CODE -->

select Project_ID, ProjectName
from Project 

CODE -->

select Trial_ID, Trial_Name
from Trial
where Project_ID = :Project_ID 

Hope this helps.

http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos

RE: populating a DBCombobox

(OP)
Yep, i tried the listsource in a lookupcombobox but something was not working right. i do not remember what. i could look again into it if this is the only solution.
Thanks.

RE: populating a DBCombobox

(OP)
I remember now...The DBlookupbox would be empty and when i would click on a name in the drop down list, it will not stick inside. The DBlookupcombobox would still be blank.

RE: populating a DBCombobox

I wouldn't use the Data Aware comboboxes for your needs, use the standard comboboxes, set their style property in the object inspector to csDropDownList (not the default style).

To populate combobox1 based on the result set of the project table use:

(Assuming you have a Query (named: ProjectQuery) with the following SQL

select project_id, project_name from project order by project_name

CODE

procedure Form1.LoadProjectNames;
begin
   ProjectQuery.Open;
   while Not ProjectQuery.Eof do
   begin
      ComboBox1.Items.Add(ProjectQuery.FieldByName('project_name').AsString;
      ProjectQuery.Next;
   end;
   ProjectQuery.Close;
   if ComboBox1.Items.Count > 0 then
      ComboBox1.ItemIndex := 0 //select the first item
end; 

call this procedure from your form's FormCreate or FormShow Method.

Then add an OnChange event for ComboBox1 that populates comboBox2

(Assuming you have another Query (named: trialQuery) with the following SQL

select trial_id, trial_name, project_id from trial where project_id =
(select project_id from project where project_name = :paramprojectname) order by trial_name

CODE

procedure Form1.CombBox1Change(Sender: TObject);
begin
   ComboBox2.Items.BeginUpdate;
   try
      ComboBox2.Clear;
      trialQuery.ParamByName('paramprojectname').Value := ComboBox1.Text;
      trialQuery.Open;
      while Not trialQuery.Eof do
      begin
         ComboBox2.Items.Add(trialQuery.FieldByName('trial_name').AsString;
         trialQuery.Next;
      end;
      trialQuery.Close;
      if ComboBox2.Items.Count > 0 then
         Combbox2.ItemIndex := 0;
   finally
      ComboBox2.Items.EndUpdate;
   end;
end; 

I didn't test the code above, so there may be errors....

RE: populating a DBCombobox

(OP)
Thank you very much..I like this approach a lot..Let me customize it.
P

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close