1. Home
  2. Computing & Technology
  3. Delphi Programming
Master-detail relationships ADO and Delphi
Page 3: Master-detail with AdoExpress components and Delphi
 More of this Feature
• Page 1: Data relationships
• Page 2: Realtions in Access

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
 Related Resources
• free DB Course.TOC
• more Database articles

   Setting up Master/Detail with ADOExpress
Creating a master-detail data form is not to much complicated. Have an empty Delphi form, and just follow the steps:

1. Select the ADO page on the Component palette. Add two TADOTable components and one TADOConnection to a form.
2. Select the Data Access page on the Component palette. Add two TDataSource components to a form.
3. Select Data Controls page on the Component palette. Place two TDbGrid components on a form. Add two DBNavigator components, too.
4. Use the ADOConnection, the ConnectionString property, to link to the aboutdelphi.bdb MS Access database, as explained in the first chapter of this course.
5. Connect DBGrid1 with DataSource1, and DataSource1 with ADOTable1. This will be the master table. Connect DBNavigator1 with DataSource1.
6. Connect DBGrid2 with DataSource2, and DataSource2 with ADOTable2. This will be the detail table. Connect DBNavigator2 with DataSource2.
7. Set ADOTable1.TableName to point to the Customers table (master).
8. Set ADOTable2.TableName to point to the Orders table (detail).

If you, at this moment, set the Active property of both ADOTable components to true, you'll notice that the entire Orders table is displayed - this is because we haven't set up the master-detail relationship yet.

Your form should look something like:

Master-detail form at design time

MasterSource and MasterFields
The MasterSource and MasterFields properties of the TADOTable component define master-detail relationships in Delphi/ADO database applications.

Field Link Designer To create a master-detail relationships with Delphi, you simply need to set the detail table's MasterSource property to the DataSource of the master table and its MasterFields property to the chosen key field in the master table.

In our case, first, set ADOTable2.MasterSource to be DataSource1. Second, activate the Field Link Designer window to set the MasterFields property: in the Detail Fields list box and the Master Fields list box select the CustNo field. Click Add and OK.

These properties keep both tables in synchronization, so as you move through the Customers table, the Orders table will only move to records which match the key field (CustNo) in the Customers table.

Each time you highlight a row and select a new customer, the second grid displays only the orders pertaining to that customer.

When you delete a record in a master table - all the corresponding record in the detail table are deleted. When you change a linked field in a record in a master table - the corresponding field in the detail table gets changed to (in as many records as needed).

Simple as that!

Stop. Note that creating a master-detail form with Delphi is not enough to support referential integrity features on two tables. Even though we can use methods described here to display two tables in a parent-child relation; if those two tables are not linked (one-to-many) within MS Access - cascading updates and deletes won't take place if you try to delete or update the "master" record.

   ADO Shaping
Shaped recordsets are an alternative to master-detail relationships. Beginning with ADO 2.0, this method is available. Shaped recordsets allow the developer to retrieve data in a hierarchical fashion. The shaped recordset adds a special "field" that is actually a recordset unto itself. Essentially, data shaping gives you the ability to build hierarchical recordsets. For instance, a typical hierarchical recordset might consist of a parent recordset with several fields, one of which might be another recordset.

For an example of the SHAPE command take a look at the shapedemo project that shiped with Delphi (in the Demos\Ado directory). You must specify the shaping provider in your connection string, by adding Provider=MSDataShape; to the beginning.

SHAPE {select * from customer}
APPEND ({select * from orders} AS Orders
RELATE CustNo TO CustNo)

Although it takes some time to master the SHAPE command that's used to create these queries, it can result in significantly smaller resultsets. Data shaping reduces the amount of traffic crossing a network, provides more flexibility when using aggregate functions, and reduces overhead when interfacing with leading-edge tools like XML.

   To the next chapter
If you need any kind of help so far, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

First page > Intro to master-detail relations > Page 1, 2, 3

DB Course Next Chapter >>
>> New ... Access datatabse with Delphi

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.