Microsoft KB Archive/161016

= ACC: How to Use DAO to Open Password-Protected Database (95/97) =

Article ID: 161016

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q161016





SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.

This article shows you how to use the OpenDatabase method to open a Microsoft Access database that has a database password. Note that this is different from opening a database that is secured with the Microsoft Access user-level security feature.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
If you want to use the OpenDatabase method to open a password-protected database, specify the database password as part of the Connect argument. The syntax to open a database with the OpenDatabase method is as follows: Set db = workspace.OpenDatabase(dbname, options, read-only, connect) NOTE: Even though the Options and Read-Only arguments of the OpenDatabase method are documented in Help as being optional arguments, you must provide them when you use the Connect argument. If you use a Connect argument and you do not provide the Options and Read-Only arguments, you receive run-time error 3031 "Not a valid password." This occurs even if the password that you provided in the Connect argument is correct. If you do not need to use a Connect argument, then you can omit the Options and Read-Only arguments.

When you use the OpenDatabase method to open a password-protected Microsoft Access database, the Connect argument of the OpenDatabase method requires the following syntax:

"MS Access;pwd= "

The following example uses the OpenDatabase method to open the sample database Northwind.mdb, which is protected with a database password of "northwind."

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database.  Open the sample database Northwind.mdb for exclusive access. To do so, click Open Database on the File menu, and then click to select the Exclusive check box in the Open dialog box. On the Tools menu, click Security, and then click Set Database password. Type "northwind" (without the quotation marks) 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.</li>  Create a module and type the following procedure:

NOTE: Substitute the correct path to Northwind.mdb on your hard drive in the following sample code. Sub OpenDB Dim db As Database Dim ws As WorkSpace Dim rst As 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 rst.MoveLast MsgBox rst!CustomerID End If      rst.Close db.Close End Sub </li> To test this procedure, type the following line in the Debug window, and then press ENTER:

OpenDB

Note that a message box displays the Customer ID of the last record in the Customers table, indicating the database was successfully opened.</li></ol>

<div class="references_section">