Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating a DBCombobox 2

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
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
 
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.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos[/url]
 
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.
 
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.
 
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....
 
Thank you very much..I like this approach a lot..Let me customize it.
P
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top