This article applies only to a Microsoft Access database (.mdb).
When you use the OpenDatabase method to open a password-protected Access database, the Connect argument of the OpenDatabase method requires the following syntax:
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
- Start Microsoft Access and open the sample database Northwind.mdb for exclusive access. To do so, click Open Database on the File menu, click the arrow on the Open button to display the options, and then click Exclusive Open.
- On the Tools menu, point to Security, and then click Set Database Password.
- Type northwind in both the Password and Verify boxes.
- Click OK to close the Set Database Password dialog box.
- Close the database.
- Create a new, blank database.
- Create a module and type the following procedure:
NOTE: Substitute the correct path to Northwind.mdb on your hard disk in the following sample code.
Dim db As DAO.Database
Dim ws As DAO.WorkSpace
Dim rst As DAO.Recordset
Set ws = DBEngine.WorkSpaces(0)
Set db = ws.OpenDatabase _
("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", _
False, False, "MS Access;PWD=northwind")
Set rst = db.OpenRecordset("Customers", dbOpenDynaset)
If rst.RecordCount > 0 Then
- To test this procedure, type the following line in the Debug window, and then press ENTER: OpenDBNote that a message box displays the Customer ID of the last record in the Customers table, indicating that the database was successfully opened.
For more information about database passwords, click Microsoft Access Help on the Help menu, type protect a microsoft access database (.mdb) file with a password or encryption in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.
Article ID: 209953 - Last Review: Jan 26, 2005 - Revision: 1