Partager via


Visual Basic Concepts

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 Northwind Traders sample database (Nwind.mdb). It assumes that the database file is located in the Samples folder on your computer.

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.

Note   This topic is part of a series that walks you through creating a simple database application that interacts with data in Nwind.mdb. It begins with the topic, Interacting with Data in a Microsoft Jet/Microsoft Access Database.

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.

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.

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.

For More Information   See Using the Data Environment Designer.

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.

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

C:\Samples\Nwind.mdb

If the database file requires you to supply a user name and password, you can also enter those on the Connection tab. You set additional connection properties by clicking the Advanced or All tabs, or test the connection by clicking Test Connection.

For More Information   For more information on connections and connection properties, see Connection Objects in "About the Data Environment Designer." And for more information on Data Links, see in "Database and Messaging Services" in the Platform SDK.

Step by Step

This topic is part of a series that walks you through creating a simple database application that interacts with data in Nwind.mdb.

To See
Go to the next step Create a Data Environment Command Object
Start from the beginning Interacting with Data in a Microsoft Jet/Microsoft Access Database