Using DAO to Connect to a Microsoft Access Database

To connect to a Microsoft Access database using the Data Access Objects (DAO), you must first set up a reference to the Microsoft Data Access Objects 3.6 Library in the References dialog box (see the section titled "Setting up References to Object Libraries" earlier in this chapter). The example procedure DAOOpenJetDatabase, illustrated below, uses the OpenDatabase method of the DBEngine object to open the Northwind database and informs the user that the database has been opened. The DBEngine object allows you to initialize the standard database engine known as Microsoft Jet Engine and open a database file (.mdb). The procedure uses the Close method to close the database file.

Sub DAOOpenJetDatabase() Dim db As DAO.Database Set db = DBEngine.OpenDatabase _

("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb") MsgBox "Northwind database has been opened." db.Close

MsgBox "Northwind database has been closed."

End Sub

0 0

Post a comment