ADO (ActiveX Data Objects)

ADOExpress Step-by-Step-from-Scratch Tech Note


ADO Master/Detail and File Locking

SummaryThese 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
B. Setup Data Module, Form, Fields and then Run the Program
C.  Run Multiple Instances of Application over Network: Optimistic Locking
D.  Pessimistic Locking <to be completed>
E.  Attempt to Run Application Without MDAC Installed

A.  Verify Existence of Access Database

Assume Access database dbdemos.mdb can be opened over a network, but verify this assumption.

  1. Verify Access database exists: From Windows Explorer, select the machine from Network Neighborhood.
  2. Select share name, and navigate to the Access file
    \Program Files\Common Files\Borland Shared\Data\dbdemos.mdb
  3. Double click to launch Access.
  4. Verify list of tables includes country, customer, employee, items, orders, parts, vendors.
  5. Close Access.

 


B.  Setup Data Module, Form, Fields and then Run the Program

  1. Start Delphi 5
  2. In the Object Inspector for Form1 set these properties:
    Caption: Master/Detail
    Name: FormMasterDetail
    Position: poScreenCenter
    Height: 450
    Width: 600
  3. File | New | Data Module | OK
    Change name from DataModule2 to DataModuleDBDemos
  4. File | Save Project As | Save In: C:\Temp (or other desired directory) |
    ScreenMasterDetail.pas | Save |
    DataDBDemos.pas | Save |
    MasterDetail.dpr | Save
  5. 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
  6. Double click on entry area for ConnectionString property.

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

  9. Select the Next button to display the Connection tab of the dialog.
  10. 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):

  11. 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.)
  12. Select the Test Connection button. The following dialog should appear:
  13.  

  14. Select the OK button three times as various dialogs close.
  15. From the ADO VCL palette tab, double click on ADOTable to add ADOTable1 to DataModuleDBDemos. Set these properties:
    Name:  ADOTableCustomer
    Connection:  ADOConnection
    TableName:  customer
  16. On the Data Access VCL tab, double click on DataSource to add DataSource1 to DataModuleDBDemos. Set these properties for DataSource1:
    Name:  DataSourceCustomer
    DataSet:  ADOTableCustomer
  17. 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)
  18. Double click on the MasterFields property data entry area. Choose CustNo from the Detail Fields list, and then from the Master Fields list.
  19. Select the Add button to join the fields, and then select the OK button. (The IndexFieldNames is also filled in with CustNo.)
  20. 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
  21. 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)
  22. 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.)
  23. Select the OK button.
  24. On the Data Access Component tab, double click on DataSource to add a new DataSource1 to DataModuleDBDemos. Set these properties:
    Name: DataSourceItems
    DataSet: ADODataSetItems
  25. At this point the DataModuleDBDemos should look something like this:
  26. 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:
  27.  
  28.  

  29. From View Unit, select ScreenMasterDetail and the OK button.
  30. 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.)
  31. Select Toggle Form/Unit.
  32. 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
  33. From the Data Controls tab, double click on DBEdit icon five times to add five DBEdit fields to the form.
  34. 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

  1. 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:

  2. 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
  3. 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
  4. From the Standard VCL palette tab, add Tlabels: LabelOrders (Top/Left = 116/8) and LabelItems (Top/Left = 272/8) with corresponding captions.
  5. Select View Unit | DataDBDemos | OK
  6. Select Toggle Form / Unit
  7. In the treeview of DataModuleDBDemos,
    select ADOTableCustomer and set Active := TRUE
    select ADOTableOrder and set Active := TRUE
    select ADOTableDataSetItems and set Active := TRUE
  8. Select View Unit | ScreenMasterDetail | OK
  9. Select Toggle Form / Unit: Data should appear in DBEdit Fields and DBGrids.
  10. Project | Build MasterDetail
  11. Run
    Using DBNavigator, advance two to CustNo=1351 and select order 1075.
  12. (Similar to Figure 12.8, Mastering Delphi 5, p. 574)

  13. Exercise various DBNavigator controls.
  14. Exit program
  15. 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.]

  1. From machine 1 on a LAN that has the EXE locally, start the MasterDetail.EXE application (double click on the EXE from Windows Explorer)
  2. 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.
  3. On both machines, advance to CustNo 1351 by clicking forward twice on the DBNavigator, and then select OrderNo 1075.
  4. On both machines, select edit mode using the DBNavigator by clicking on this button:
  5. On machine 2, change the city/state to Topeka, KS and post the change (the checkmark Dbnavigator button).
  6. 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.
  7. 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.

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