How To Connect a Delphi Application to Access

Delphi simplifies the common programming task a developer has to do in his everyday work. Database Application development is one of the strength of Delphi.  Its principles and tools have been used by other programming languages and IDEs: Visual Studio's non-visual database components were inspired from Delphi.

Step 1

I- Create an new application:

1 - Create a new blank application project: File-->New-->Application

Step 2

II- Set the connection to your ACCESS database:

2 - Click on the "ADO" (delphi7) or "dbGo" (BDS2006) tab on the component palette

3 - Click once on the "ADO Connection" component:  to identify each component.  Just point your mouse over any component and a Hint will show the component name.

4 -  Click once on the form to drop a new "ADO Connection."  The new component is named by default "ADOConnection1."

5 - Set the component main properties using the "Object inspector:"  First click on the component, then choose the property you would modify from the "Object inspector."

To show the "Object Inspector," click on "View" in Delphi menu, then click on "Object Inspector," or simply hit the "F11" button on your keyboard.

The most important properties you'll have to modify are Connection string and LoginPrompt. Click on "connection string" in the object inspector, then click on the three dots in front on it to show the "ADO Connection wizard."  Follow the wizard to build the connection string.

First, click on "build" in the bottom right of the Wizard window. Then choose "Microsoft jet 4.0 OLEDB Provider."  Hit next. In the following window, browse to the access database you will use for your application.

To test the connection we've just created, click on the "Test Connection" button at the bottom right of the window. If the test is successful, click on "OK," else verify the parameters you've selected again (Database path and Database Driver).

6 - Activate the connection by setting the "Connected" property to true in the Object inspector.

Step 3

III- Adding and setting up a Table object:

1 - Now that you've set up your ADO Connection, go back to the ADO/dbGo tab on your component palette. Click on the "ADO Table" component, then click again on the form. The new component is named by default "ADOTable1."

2 - Set the "connection" property of your new "ADO Table" object to the connection object you created earlier in step one by selecting it in the drop down list ("ADOConnection1").

3 - Choose a table to link to your new "ADO Table" object. First, click on "Table name" property in the object inspector. Then, click on the arrow to expand the list. This list contains the names of all the tables in your database. Select the table you would like to edit.

4 - Set the "LoginPrompt" property to false.

5 - Activate the "Table" by setting the "Active" property to true in the Object inspector.


Step 4

IV- Adding a "datasource" to the project:

1 -  The datasource object, available through the "DB Access" tab on the component palette will act as the connection between the data-aware components we will use and the table objects representing the tables in our database.

A Data-aware component is a one that can show the data from our database. The typical Data-aware component you will be using is the DBGrid, which we will use later.

To add a datasource to the application switch to the "DB Access" tab on your component palette. Then add a "Datasource" component as you did with the objects we did use earlier. First click on the component, then click again on the form.

2 - To set up our datasource component, first click on it. The new component is named by default "Datasource1". Then, modify the main property: "DataSet". This property will usually point to the "DataSet" component representing our database objects (tables or queries/Views).

In our case, choose the table object we created earlier ("ADOTable1")

Step 5

V- Adding data-aware components to our application.

1 - First, switch to the "DB Controls" tab on your component palette.

2 - Click on the "DBGrid" object, then click once again on the form. The new component is named by default "DBGrid1".

3 - Set the "DataSource" property of the DBGrid to the datasource we created earlier ("DataSource1").

The DBGrid displays the records of your Table.

Step 6

VI- Add navigation and edition features to your application.

1 - Switch back to the "DBControls" tab.

2 - Add a "DBNavigator" component to your form.

3 - Link it to your datasource ("DataSource1") by setting its datasource property through the "Object Inspector".

4 - Run your application either by hitting F9 or clicking on the play button in Delphi's menu bar.

That's it!!  You've just made your first Data-Aware application with Delphi. Don't be afraid to try other DB-Aware controls and components. Have fun with Delphi :)


Share this article!

Follow us!

Find more helpful articles: