ADO (ActiveX Data Objects)

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


DBF Files -- With or Without ADO

Summary:  These instructions show how to connect to a DBF (dBase) file with or without the ADO.  Images can only be accessed when ADO is not used.

A. Verify DBF Table Exists
B.  Open DBF Table Without Using ADO
C.  Open DBF Table Using ADO

A.  Verify DBF Table Exists

  1. Start Delphi 5
  2. Tools | Database Desktop
  3. File | Open | Table | C:\Program Files\Common Files\Borland Shared\Data\animals.DBF | Open
  4. Close Database Desktop

B.  Open DBF Table Without Using ADO

  1. Start Delphi 5 (if necessary)
  2. In the Object Inspector for Form1, change these properties:
    Caption: DBF Table
    Name: FormDBFDemo
    Position: poScreenCenter
    Height: 375
    Width: 500
  3. File | New | Data Module | OK
    Change name from DataModule2 to DataModuleDBF
  4. File | Save Project As | Save in: C:\Temp (or other desired directory) |
    ScreenDBFDemo.pas (instead of unit1.pas) | Save |
    DataDBF.pas (instead of unit2.pas) | Save |
    DBFDemo.dpr (instead of project1.dpr) | Save

    (Note: My convention for a program XXXX is to have a FormXXXX defined in the ScreenXXXX.PAS unit with a XXXX.DPR project file. Likewise, a DataModuleYYYY is saved in a DataYYYY.PAS unit.)
  5. With the Components tab visible and selected on the DataModuleDBF "form," select the Data Access tab on the VCL Palette. Double click on the Table icon. Configure Table1 with these properties:
    Name: TableAnimals
    DatabaseName: C:\Program Files\Common Files\Borland Shared\Data (don't use an alias)
    TableName: animals.dbf
  6. From the Data Access tab on the VCL Palette, double click on the Data Source icon. Configure DataSource1 with these properties:
    Name: DataSourceAnimals
    DataSet: TableAnimals
  7. From View Unit, select ScreenDBFDemo. | OK
  8. In the Implementation section of FormDBFDemo, add "USES DataDBF;"
  9. Select Toggle Form / Unit
  10. From the Data Controls tab on the VCL Palette, double click on the DBGrid icon to place an instance on FormDBFDemo. Configure DBGrid1 with the following properties:
    Name: DBGridAnimals
    DataSource: DataModuleDBF.DataSourceAnimals
    Options.dgRowSelect: TRUE
  11. From the Data Controls tab on the VCL Palette, double click on the DBImage icon to place an instance on FormDBFDemo. Configure DBImage1 with the following properties:
    Name: DBImageAnimal
    DataSource: DataModuleDBF.DataSourceAnimals
    DataField: BMP
    Width: 200
    Height: 150
    Stretch: TRUE
  12. Select View Unit. Select DataDBF. OK. Select Toggle Form/Unit. Select icon for TableAnimals. In the object inspector, set Active = TRUE.
  13. Select View Unit. Select ScreenDBFDemo. OK. Select Toggle Form/Unit. Verify data are shown in design mode:
  14. Project | Build DBFDemo | Run
    Scroll down or up to select and display pictures of various animals in the DBF table.
    Press the "X" at the upper right to close the application.
  15. Select View Unit. Select DataDBF. OK. Select Toggle Form/Unit. Select the Data Diagram tab on the "form."
  16. Drag the icon in the tree view for TableAnimals to the Data Diagram area.
    Drag the icon for DataSourceAnimals to the Data Diagram area.
    Drag the icon for the Constraints to the Data Diagram area.
    Drag the icon for the FieldDefs to the Data Diagram area

    Resize boxes as necessary in the Data Diagram area so all text appears.
    Reposition boxes as desired.

    Using the tool palette at the left of the diagram area, add a "ABC" comment with the text:
    C:\Program Files\Common Files\Borland Shared\Data\animals.DBF
  17. File | Close All | Yes (to "Save Changes")

C.  Open DBF Table Using ADO

  1. Start Delphi 5 (if necessary)
  2. In the Object Inspector for Form1:
    Caption: DBF Table Via ADO
    Name: FormDBFADODemo
    Position: poScreenCenter
    Height: 200
    Width: 500
  3. File | New | Data Module | OK
    Change name from DataModule2 to DataModuleADODBF
  4. File | Save Project As | Save in: C:\Temp (or other desired directory) |
    ScreenDBFADODemo.PAS (instead of unit1.pas) | Save |
    DataDBFADO.PAS (instead of unit2.pas) | Save |
    DBFADODemo.PAS (instead of project1.dpr) | Save
  5. With the Components Tab visible and selected on the DataModuleADODBF "Form," select the ADO tab on the VCL Palette. Double click on the ADOConnection icon. Configure ADOConnection1 with these properties:
    Name: ADOConnection
    Login Prompt: False
  6. Double click on the ellipses in the Connection String entry area. Select Use Connection String and press the Build button. Select Microsoft OLE DB Provider for ODBC Drivers.

  7. Select the Next button.

  8. I'm not really sure why you would ever want to select dBASE Files as the data source name. Instead, select the Use connection string radio button and select the Build button:
  9.  

  10. Select the dBASE Files option:
  11. Press the OK button.

  12. A Select Directory dialog appears. Specify the directory:
    C:\Program Files\Common Files\Borland Shared\Data
  13. Press the OK button three times.

  14. From the ADO VCL tab, double click on the ADOTable to place an instance on DataModuleADODBF. Specify the following properties:
    Name: ADOTableAnimals
    Connection: ADOConnection
    TableName: animals
  15. From the Data Access tab on the VCL Palette, double click on the Data Source icon. Configure DataSource1 with these properties:
    Name: DataSourceAnimals
    DataSet: ADOTableAnimals

  16. From View Unit, select ScreenDBFADODemo. | OK
  17. In the Implementation section of FormDBFDemo, add "USES DataDBFADO;"
  18. Select Toggle Form / Unit
  19. From the Data Controls tab on the VCL Palette, double click on the DBGrid icon to place an instance on FormDBFDemo. Configure DBGrid1 with the following properties:
    Name: DBGridAnimals
    DataSource: DataModuleADODBF.DataSourceAnimals
    Options.dgRowSelect: TRUE
  20. Select View Unit. Select DataDBFADO. OK. Select Toggle Form/Unit. Select icon for ADOTableAnimals. In the object inspector, set Active = TRUE.
  21. Select View Unit. Select ScreenADODBFDemo. OK. Select Toggle Form/Unit. Verify data are shown in design mode:

NOTE: BMP field is missing when accessed via ADO.

  1. Project | Build DBFADODemo | Run
    Scroll down or up to select data about various animals in the DBF table.
    Press the "X" at the upper right to close the application.
  2. Select View Unit. Select DataDBFADO. OK. Select Toggle Form/Unit. Select the Data Diagram tab on the "form."
  3. Drag the icon in the tree view for ADOConnection to the Data Diagram area.
    Drag the icon in the tree view for ADOTableAnimals to the Data Diagram area.
    Drag the icon for DataSourceAnimals to the Data Diagram area.
    Drag the icon for the Fields to the Data Diagram area

    Resize boxes as necessary in the Data Diagram area so all text appears.
    Reposition boxes as desired.

    Using the tool palette at the left of the diagram area, add a "ABC" comment with the text:
    C:\Program Files\Common Files\Borland Shared\Data\animals.DBF
  4. File | Close All | Yes (to "Save Changes")

Links Verified 24 Jan 2000
Updated 18 Feb 2002


since 24 Jan 2000