ID: Q161016
The information in this article applies to:
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.
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=<password>"
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.
1. 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.
2. On the Tools menu, click Security, and then click Set Database password.
3. Type "northwind" (without the quotation marks) in both the Password and
Verify boxes.
4. Click OK to close the Set Database Password dialog box.
5. Close the database.
6. Create a new, blank database.
7. 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
8. 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.
For more information about the OpenDatabase method, search the Help Index for "OpenDatabase method."
For more information about database passwords, search the Help Index for "passwords, database," or ask the Microsoft Access 97 Office Assistant.
Additional query words: pwd secure
Keywords : kbprg MdlDao
Version : 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto
Last Reviewed: November 21, 1998