Monday, April 27, 2009

Building an example master/detail form

The following steps create a simple form in which a user can scroll through customer records and display all orders for the current customer. The master table is the CustomersTable table, and the detail table is OrdersTable.

1 Place two TTable and two TDataSource components in a data module.
2 Set the properties of the first TTable component as follows:

DatabaseName: BCDEMOS
TableName: CUSTOMER
Name: CustomersTable

3 Set the properties of the second TTable component as follows:

DatabaseName: BCDEMOS
TableName: ORDERS
Name: OrdersTable

4 Set the properties of the first TDataSource component as follows:

Name: CustSource
DataSet: CustomersTable

5 Set the properties of the second TDataSource component as follows:

Name: OrdersSource
DataSet: OrdersTable

6 Place two TDBGrid components on a form.
7 Choose File|Include Unit Hdr to specify that the form should use the data module.
8 Set the DataSource property of the first grid component to
“CustSource”, and set the DataSource property of the second grid to “OrdersSource”.
9 Set the MasterSource property of OrdersTable to “CustSource”. This links the CUSTOMER table (the master table) to the ORDERS table (the detail table).

10 Double-click the MasterFields property value box in the Object Inspector to invoke the Field Link Designer to set the following properties:

In the Available Indexes field, choose CustNo to link the two tables by the CustNo field.
Select CustNo in both the Detail Fields and Master Fields field lists.
Click the Add button to add this join condition. In the Joined Fields list,
“CustNo -> CustNo” appears.
Choose OK to commit your selections and exit the Field Link Designer.

11 Set the Active properties of CustomersTable and OrdersTable to true to display data in the grids on the form.
12 Compile and run the application.

If you run the application now, you will see that the tables are linked together, and that when you move to a new record in the CUSTOMER table, you see only those records in the ORDERS table that belong to the current customer.

No comments:

Post a Comment