ADO Master/Detail and File Locking
Summary: These instructions show
how to use ADO to connect to an Access database over a local area network. A
Master/Detail relationship is demonstrated. Multiple applications accessing the same
database are explored. Database locking (optimistic versus pessimistic) is
demonstrated.
Part of this example is based on "Master/Detail Structure" in Mastering Delphi
5, pp. 572-574, including Figure 12.8.
A. Verify Existence of Access Database
Assume Access database dbdemos.mdb can be opened over a network, but
verify this assumption.
- Verify Access database exists: From Windows Explorer, select the machine
from Network Neighborhood.
- Select share name, and navigate to the Access file
\Program Files\Common Files\Borland Shared\Data\dbdemos.mdb
- Double click to launch Access.
- Verify list of tables includes country, customer, employee, items, orders, parts,
vendors.
- Close Access.
B. Setup Data Module, Form, Fields and then
Run the Program
- Start Delphi 5
- In the Object Inspector for Form1 set these properties:
Caption: Master/Detail
Name: FormMasterDetail
Position: poScreenCenter
Height: 450
Width: 600
- File | New | Data Module | OK
Change name from DataModule2 to DataModuleDBDemos
- File | Save Project As | Save In: C:\Temp (or other desired directory) |
ScreenMasterDetail.pas | Save |
DataDBDemos.pas | Save |
MasterDetail.dpr | Save
- With the Components Tab visible and selected on the DataModuleDBDemos
"form," select the ADO tab on the VCL palette. Double click on the ADOConnection
icon to add an instance to the form. Configure ADOConnection1 with these
properties:
Name: ADOConnection
Login Prompt: False
- Double click on entry area for ConnectionString property.

- Press the Build button.
- On the Provider tab, select Microsoft Jet 4.0 OLE DB Provider.

- Select the Next button to display the Connection tab of the dialog.

- Select the database name by pressing on the button with the ellipses
("
"). Press the up arrow folder icon till Network Neighborhood is
seen. Select Network Neighborhood and the Open button (or just
double click on the Network Neighborhood icon):

- After selecting the machine ("Earlg2" in this case), select the share name
("C" in this case) and then the Access file: \Program Files\Common
Files\Borland Shared\Data\dbdemos.mdb. Press the Open button.
Notice the UNC (universal naming convention) name appears in the database name field. (Use
of the UNC name will allow sharing this database by any machine on the local area
network.)

- Select the Test Connection button. The following dialog should appear:

- Select the OK button three times as various dialogs close.
- From the ADO VCL palette tab, double click on ADOTable to add ADOTable1
to DataModuleDBDemos. Set these properties:
Name: ADOTableCustomer
Connection: ADOConnection
TableName: customer
- On the Data Access VCL tab, double click on DataSource to add DataSource1
to DataModuleDBDemos. Set these properties for DataSource1:
Name: DataSourceCustomer
DataSet: ADOTableCustomer
- From the ADO component palette tab, double click on ADOTable to add a new ADOTable1
to DataModuleDBDemos. Set these properties:
Name: ADOTableOrder
Connection: ADOConnection
TableName: orders
MasterSource: DataSourceCustomer (only item in pull down selection)
- Double click on the MasterFields property data entry area. Choose CustNo
from the Detail Fields list, and then from the Master Fields list.

- Select the Add button to join the fields, and then select the OK button.
(The IndexFieldNames is also filled in with CustNo.)
- On the Data Access Component tab, double click on the DataSource icon to
add a new DataSource1 to DataModuleDBDemos. Set these properties:
Name: DataSourceOrder
DataSet := ADOTableOrder
- From the ADO component palette tab, double click on ADODataSet to add an ADODataSet1
to DataModuleDBDemos. Set these properties:
Name: ADODataSetItems
Connection: ADOConnection
DataSource: DataSourceOrder
CommandType: cmdTable
CommandText: items (select from drop-down list)
- Double click on the MasterFields property data entry area. Choose OrderNo
from the Detail Fields list, and then from the Master Fields list. Select
the Add button. (IndexFieldNames is also filled in with OrderNo.)

- Select the OK button.
- On the Data Access Component tab, double click on DataSource to add a
new DataSource1 to DataModuleDBDemos. Set these properties:
Name: DataSourceItems
DataSet: ADODataSetItems
- At this point the DataModuleDBDemos should look something like this:

- Select the DataDiagram tab on DataModuleDBDemos and drag the icons from
the tree view to the Data Diagram area: ADOConnection, ADOTableCustomer,
DataSourceCustomer, ADOTableOrder, DataSourceOrder, ADODataSetItems, DataSourceItems.
Resize and re-arrange the boxes to form a diagram like the following:
-

- From View Unit, select ScreenMasterDetail and the OK button.
- In the Implementation section of the ScreenMasterDetail unit, add
"USES DataDBDemos;"
(This statement allows the items on the DataModuleDBDemos to be used via the IDE.)
- Select Toggle Form/Unit.
- From the Data Controls VCL tab, double click on the DBNavigator icon to
add DBNavigator1 to FormMasterDetail. Set these properties:
Name: DBNavigator
DataSource: DataModuleDBDemos.DataSourceCustomer
Top: 8
Left: 170
- From the Data Controls tab, double click on DBEdit icon five times to add
five DBEdit fields to the form.
- Assign these properties to the DBEdit fields:
| property |
DBEdit1 |
DBEdit2 |
DBEdit3 |
DBEdit4 |
DBEdit5 |
| Name |
DBEditCustNo |
DBEditCompany |
DBEditCity |
DBEditState |
DBEditCountry |
| Width |
100 |
200 |
200 |
200 |
200 |
| DataSource |
DataModuleDBDemos.DataSourceCustomer |
| DataField |
CustNo |
Company |
City |
State |
Country |
| Top |
51 |
76 |
51 |
76 |
101 |
| Left |
90 |
354 |
- From the Standard VCL palette tab, add a TLabel near each DBEdit
field. Change names to LabelXXX, where XXX is the DataField name. Likewise,
the caption fields of these labels are XXX. FormMasterDetail should look something
like this:

- From the Data Controls VCL tab, double click on DBGrid to add the
instance, DBGrid1, to FormMasterDetail. Set these properties:
Name: DBGridOrder
DataSource: DataModuleDBDemos.DataSourceOrder
Options.dgRowSelect: TRUE
Top: 132
Left: 8
Height: 129
Width: 575
- From the Data Controls tab, double click on DBGrid to add a new DBGrid1
to FormMasterDetail. Set these properties:
Name: DBGridItems
DataSource: DataModuleDBDemos.DataSourceItems
Options.dgRowSelect: TRUE
Top: 288
Left: 8
Height: 129
Width: 575
- From the Standard VCL palette tab, add Tlabels: LabelOrders
(Top/Left = 116/8) and LabelItems (Top/Left = 272/8) with corresponding captions.
- Select View Unit | DataDBDemos | OK
- Select Toggle Form / Unit
- In the treeview of DataModuleDBDemos,
select ADOTableCustomer and set Active := TRUE
select ADOTableOrder and set Active := TRUE
select ADOTableDataSetItems and set Active := TRUE
- Select View Unit | ScreenMasterDetail | OK
- Select Toggle Form / Unit: Data should appear in DBEdit Fields and DBGrids.
- Project | Build MasterDetail
- Run
Using DBNavigator, advance two to CustNo=1351 and select order 1075.

(Similar to Figure 12.8, Mastering Delphi
5, p. 574)
- Exercise various DBNavigator controls.
- Exit program
- File | Close All | Yes (to "Save Changes") | Exit (Delphi 5)
C. Run Multiple Instances of Application
over Network: Optimistic Locking
Assume two machines are running Windows 98 with the ADO redistributable installed.
[To install ADO support inWindows 95, see the Tech Note "Running ADO Applications in Windows 95." Also,
see Section E for a description of what happens if ADO support is not
present.]
- From machine 1 on a LAN that has the EXE locally, start the MasterDetail.EXE
application (double click on the EXE from Windows Explorer)
- From machine 2 on a LAN, start the MasterDetail.EXE application (use
"up" in Windows Explorer till you see Network Neighborhood. Select
the machine, share name and then directory, e.g., C:\Temp\MasterDetail.doc. Then
double-click on the MasterDetail.EXE file to start the program.
- On both machines, advance to CustNo 1351 by clicking forward twice on the DBNavigator,
and then select OrderNo 1075.
- On both machines, select edit mode using the DBNavigator by clicking on this
button:

- On machine 2, change the city/state to Topeka, KS and post the change (the checkmark Dbnavigator
button).
- On machine 1, change the city/state to Falls Church, VA and post the change. Because of
optimistic locking (the default), the following message appears.

- On machine 1, click OK to the above message and then Cancel (the
"X") on the DBNavigator button. Click the Refresh button to see
Topeka, Kansas updated to machine 1.

- On machine 1, change the City back to Kato Paphos and blank the State field.
Then post the results (the checkbox button). Press the Refresh DBNavigator button
on machine 2 to see change from machine 1.
D.
Pessimistic Locking
<to be completed>
E. Attempt to Run Application Without MDAC
Installed
If you attempt to run an Delphi ADO application in Window 95, you'll see
these messages:


To install ADO support inWindows 95, see the Tech Note "Running ADO Applications in Windows 95."
Links Verified 26 Jan 2000
Updated 18 Feb 2002

since 26 Jan 2000