The steps in this How To show how to build a form containing two tables: a master table and a detail table, linked one-to-many.
Difficulty Level: average Time Required: 10 minutes
Here's How:
- Start Delphi and select File | New Application...
- Select Data Access page on the Component palette. Place two TTable components on a form. Place two TDataSource components on a form.
- Select Data Controls page on the Component palette. Place two TDbGrid components on a form.
- Set the DatabaseName of the TTable components (Table1 and Table2) to the name of the database to access. Select DBDemos.
- Set the TableName property to the name of the table in the database. Select customer.db for the master table: Table1.
- Do the same with the child table. Select orders.db for the Table2.
- Select the first TDataSource (DataSource1) component and set DataSet property to the name of the table component, that is: Table1.
- Do the same with the second TDataSource (DataSource2) component and set DataSet property to the name of the table component, that is: Table2.
- Select the first TDbGrid (DBGrid1) component and set the DataSource property to the name of the data source component, i.e. DataSource1. The same for, that is, TDbGrid2.DataSource:=DataSource2.
- Set the MasterSource property of the Table2 component to the DataSource1.
- Set the IndexFieldName property of the Table2 component to the CustNo.
- Click the ellipsis button next to the MasterFields property of the Table2. This will invoke the Field Link Designer.
- In the Detail Fields list the select CustNo, in the MasterFields list select CustNo. Click Add. Click Ok.
- Set the Active property of both table components to True.
- Browse through the records.
- The most important thing in master-detail relationship is the link between the tables. For each record in the master table, the form displays all corresponding records in the detail table.
- Important detail table properties are: IndexFieldNames, MasterFields and MasterSource. For more info look in Delphi Help files.
Related Features:
- Database Form Wizard - Developing my own, first Delphi database application, using Database Form Wizard.
- Coloring DBGrid - How to enhance the functionality of a TDBgrid component using colors.
- How To Create a Simple Database Display Form
- More Delphi Programming HOW TOs

