Setting up a Master/Detail relationship
Here is an example for those who have not yet set up a master/detail relationship with the Delphi database components.
For this example you will need to start
a new application (Project) and save it.
Next add two DBGrid components from the
Data Controls components tab.
Set the Align property in the Object
Inspector
for DBGrid2 to alBottom, so that DBGrid2 is at the bottom of the Form1.
Next do the same with DBGrid1, so that
DBGrid1 is on top of DBGrid2.
Next go to the Data Access component tab and add a table to your form, this is Table1, also add a DataSource component to Form1.
Now add another Table component and another DataSource component.
Make sure Table1 is selected then in the Object Inspector click on the DatabaseName drop-down list, select DBDemos, then further down in the Object Inspector click on the TableName drop-down list and select customer.db.
Double-click on Table1 and you will see the Fields editor, right-click and select Add All Fields, then close the Fields editor.
Click on DataSource1 and in the Object Inspector click on DataSet and select Table1.
Next connect DBGrid1 to DataSource1 and DBGrid2 to DataSource2 with the DataSource property for each DBGrid.
Now lets set up Table2.
Make sure Table2 is selected then in the Object Inspector click on the DatabaseName drop-down list, select DBDemos, then further down in the Object Inspector click on the TableName drop-down list and select orders.db.
Double-click on Table2 and you will see the Fields editor, right-click and select Add All Fields, then close the Fields editor.
Click on DataSource2 and in the Object Inspector click on DataSet and select Table2.
Now comes the interesting part.
MasterFields and MasterSource properties:
The MasterFields and MasterSource properties can be use for one-to-many relationships between two tables.
With Table2 selected in the Object Inspector, go to the MasterSource property and choose 'DataSource1' from the drop down list.
Now, double-click in the MasterFields property for Table2.
This is the Field Link designer which
gives
you a way to link (or join) master and detail tables.
At the top of the Field Link Designer
you will see a combo box labeled Available Indexes, these are Primary
and
CustNo, select CustNo.
You will now see that the Detail Fields
list box has CustNo listed, to the right is another list box called
Master
Fields.
In the Master Fields list box select
CustNo.
Now both the Detail Fields list box and the Detail Fields list box should have CustNo selected, next click on the Add button and this will create the relationship we want. Now click OK.
Click on Table1 and change its Active property to True in the Object Inspector then do the same for Table2.
Table1 shows all the customers and Table2 shows all the orders for those customers.
Lets add some simple filtering, we will use a ComboBox for this. So go to the Standard component palette tab and then click on a ComboBox component and add it to the top of Form1.
With ComboBox1 selected go to Events in the Object Inspector and double-click on the OnChange event, then add this code:
procedure
TForm1.ComboBox1Change(Sender:
TObject);
begin
if (ComboBox1.Text
= '') or (ComboBox1.Text = ' ') then Exit;
Table1.Filter :=
'Company
=' + QuotedStr(ComboBox1.Text);
Table1.Filtered :=
True;
end;
Go back to the Properties tab for
ComboBox1
and click on the Items property, add these words to the StringList
Editor:
(Use copy and paste to
save
time)
Adventure Undersea
Blue Jack Aqua Center
Blue Sports Club
Davy Jones' Locker
Frank's Divers Supply
Island Finders
Jamaica SCUBA Center
Ocean Paradise
Sight Diver
Tom Sawyer Diving Centre
Unisco
VIP Divers Club
Also change the ComboBox1 sorted property to True.
To turn the Filter off, put a TButton
on
your form and double-click on it.
Add this code to the OnClick event:
procedure TForm1.Button1Click(Sender:
TObject);
begin
Table1.Filtered :=
False;
end;
Run the program.
Using ComboBox1 select "VIP Divers Club".
Have a good look and you will see the advantages to using Master/Detail relationships.
Both tables are now linked together, by doing this you are able to
find
a companies name and then see all the orders they have made.
This linking relationship can be very usefull in many database
applications,
we hope the example above helps give you some ideas.
If you use the filter ComboBox and see nothing in DBGrid2, make sure there are no spaces after the items in the StringList Editor.