Masterdetail relationships ADO and Delphi

Page 3: Master-detail with AdoExpress components and Delphi

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:

More of this Feature

  • Page 1: Data relationships
  • Page 2: Realtions in Access jy Printer 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
  • a href=""> more Database articles
  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.

  1. Use the ADOConnection, the ConnectionString property, to link to the aboutdelphi.bdb MS Access database, as explained in the first chapter of this course.
  2. Connect DBGridl with DataSourcel, and DataSourcel with

Game Programming Graphics Internet/Intranet Mathematics Multimedia Delphi Pascal/IDE Screen saver dev. VCL Using VCL Write/Enhance Web Services Windows/Shell/API Kylix (D on Linux)

Coding standards Books/Publications Dev. Utilities FAQs/Tips/Tricks Free Code Projects Humor and Fun Icons and Glyphs Jobs and Offers Training/CD/Video VCL & Tools

Subject Library

All articles on this topic

  1. This will be the master table. Connect DBNavigatorl with DataSourcel.
  2. Connect DBGrid2 with DataSource2, and DataSource2 with ADOTable2. This will be the detail table. Connect DBNavigator2 with DataSource2.
  3. Set ADOTablel.TableName to point to the Customers table (master).
  4. 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:

MasterSource and MasterFields

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

Stay up-to-date!

Subscribe to our newsletter.

Enter Email


> Free Credit Report

> Free Psychics

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

All graphics (if any) in this feature created by Zarko Gajic.

More Delph

  • Learn another routine every day - RTL Quick Reference.
  • Download free source code applications and components.
  • Talk about Delphi Programming, real time. Start a chat now!
  • Link to the Delphi Programming site from your Web pages.
  • Tutorials, articles, tech. tips by date: 2001120001199911998 or by TOPIC.
  • NEXT ARTICLE: Real-time 2D particle systems (with gravitation!).

This tutorial is going to discuss a library of Delphi routines that will help you create your own particle systems. The term system is defined to mean, 'A group of interacting, interrelated, or interdependent elements forming a complex whole'. You can use the particle to represent a cannonball being shot out of a cannon - in trajectory games.

Stay informed with all new and interesting things about Delphi (for free).

Subscribe to the About Programming Newsletter

Name Email


Got some code to share? Got a question? Need some help?

Recent Discussions

Delphi Programming

Join these forum conversations

  • a href="">Extracting digital audio from cd
  • a href="">Delphi disadvantages compared to VS.Net

Keep An Application Always On Top

Delphi7 adoblob problem Searching in a text file or wide string

Was this article helpful?

+1 0

Post a comment