Step 1: Create a Connection to a Microsoft Access Database File

When you work with data in a Microsoft Access database, you must first create a connection to a database file. The easiest way to create a connection to a Microsoft Access file is to create a data environment using the Data Environment designer.

This topic shows how to create a connection to the Phone List sample database (Employees.mdb). It assumes that the database file is located on the Student Disk downloaded from the publisher site by using the back cover of the text.

To create a connection to a Microsoft Jet/Microsoft Access database file

  1. Add a Data Environment designer to your project.
  2. Set connection properties for the designer.

Add a Data Environment Designer to Your Project

A Data Environment designer provides an easy way to create connections to many types of databases. To add a designer to your project, click Add Data Environment on the Project menu. Visual Basic loads a data environment and adds a Connection object to the data environment. Click the DataEnvironment1 icon in the Data Environment Window, change its name property in the property window to datEmployeesEnvironment and press Enter key.

If the Data Environment designer is not available on the Project menu, add it to your Visual Basic environment. Click Components on the Project menu, click the Designers tab, and then click the check box next to its name in the list of designers.

Click to see How to add Data Enviromnent

Note The first four kinds of ActiveX designers loaded for a project are listed on the Project menu. If more than four designers are loaded, the later ones will be available from the More ActiveX Designers submenu on the Project menu.

Set Connection Properties

You establish the connection to your data source by setting properties in the Data Link Properties dialog box.  To display this dialog box, right-click the Connection object in your data environment and then choose Properties from the shortcut menu. Select an OLE DB provider on the Provider tab of the dialog box. Then click Next and enter connection information on the Connection tab of the dialog box. The layout of the Connection tab varies, depending on the OLE DB provider you select on the Provider tab.

Click to see How to set Connection Properties

For example, to connect to a Microsoft Jet/Microsoft Access database such as the Employees sample database, select the Microsoft Jet 3.51 OLE DB provider on the Provider tab. On the Connection tab, enter the path to the database file; for example:

A:\Data\Employees.mdb

You can test the connection by clicking Test Connection Button.

Click to see How to set Connection Path and Test link

Go to Step 2: