Building an example master/detail form

From InterBase

Go Up to Creating master/detail forms

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 SalesTable.

  1. To create a VCL project, select File > New > VCL Forms Application - Delphi.
  2. Add a data module to the project:
    In the Project Manager, right-click the project and select Add New... > Other... > Data Module.
  3. Drop two of each of these components in the data module: TIBTable, TIBDatabase, TIBTransaction, and TDataSource.
  4. Set the Name of the first TIBDatabase component to CustDatabase.
  5. Double-click the CustDatabase component to open the Database Component Editor:
    1. Set the Database parameter to:
      C:\Users\Public\Documents\Embarcadero\Studio\17.0\Samples\Data\employee.gdb (location of the database)
    2. Set the User Name to sysdba.
    3. Set the Password to masterkey.
    4. Uncheck the Login Prompt box.
    5. Click Test to check is the connection is successful.
    6. Click OK to close the DataBase Component Editor.
  6. Set the Name of the first TIBTransaction component to CustTransaction and the DefaultDatabase to CustDatabase.
  7. Set the DefaultTransaction of the CustDatabase component to CustTransaction.
  8. Set the properties of the first TIBTable component as follows:
    1. Database as CustDatabase.
    2. Transaction as CustTransaction.
    3. TableName as CUSTOMER.
    4. Name as CustomersTable.
  9. Set the Name of the second TIBDatabase component to SalesDatabase.
  10. For SalesDatabase, open the Database Component Editor, and fill it as in Step 5.
  11. Set the Name of the second TIBTransaction component to SalesTransaction and the DefaultDatabase to SalesDatabase.
  12. Set the DefaultTransaction of the SalesDatabase component to SalesTransaction.
  13. Set the properties of the second TIBTable component as follows:
    1. Database as SalesDatabase.
    2. Transaction as SalesTransaction.
    3. TableName as SALES.
    4. Name as SalesTable.
  14. Set the properties of the first TDataSource component as follows:
    1. Name as CustSource.
    2. DataSet as CustomersTable.
  15. Set the properties of the second TDataSource component as follows:
    1. Name as SalesSource.
    2. DataSet as SalesTable.
  16. Now, on the form, place two TDBGrid components.
  17. Choose File>Use Unit to specify that the form should use the data module.
  18. Set the DataSource property of the first grid component to DataModule2.CustSource, and set the DataSource property of the second grid to DataModule2.SalesSource.
  19. On the data module, set the MasterSource property of SalesTable to CustSource. This links the CUSTOMER table (the master table) to the ORDERS table (the detail table).
  20. Double-click the MasterFields property value box in the Object Inspector to invoke the Field Link Designer to set the following properties:
    1. Select CustNo in both the Detail Fields and Master Fields field lists.
    2. Click the Add button to add this join condition. In the Joined Fields list, CustNo -> CustNo appears.
    3. Choose OK to commit your selections and exit the Field Link Designer.
  21. Set the Active properties of CustomersTable and SalesTable to True to display data in the grids on the form. At this point, you can see data on the TDBGrid components.
  22. 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 SALES table that belong to the current customer.

Advance To: