For a Microsoft Access 97 version of this article,
see
161016.
Advanced: Requires expert coding, interoperability, and multiuser
skills.
This article applies only to a Microsoft Access database (.mdb).
IN THIS TASK
SUMMARY
By using the
OpenCurrentDatabase method, you can programmatically open a Microsoft Access database
within the Microsoft Access user interface. However, the
OpenCurrentDatabase method does not provide a parameter for specifying a password for
password-protected databases. Therefore, the user is automatically prompted to
enter the database password if one exists.
This article describes how
to programmatically open a password-protected database in the Microsoft Access
user interface without user intervention.
Data Access Objects (DAO)
allows you to specify a database password when opening a password-protected
database. By using the
DBEngine property of the instance of Microsoft Access that your code
creates, it is possible to use DAO to specify the password of the database.
After the database password has been validated by the Microsoft Jet database
engine, you can use the
OpenCurrentDatabase method to open the database in the Microsoft Access user
interface without user intervention.
back to the top
Step-by-Step Example
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 2000.
- Open the sample database Northwind.mdb for exclusive use.
To open the database for exclusive use, click Northwind.mdb in the Open dialog box, click the arrow next to the Open button, and then click Open Exclusive.
- On the Tools menu, point to Security, and then click Set Database Password.
- Type nwind in the Password and Verify boxes, and then click OK.
- Close the sample database Northwind.mdb.
- Open the sample database Northwind.mdb to verify that you
receive a prompt to enter the database prompt.
- Click Cancel to prevent the database from opening.
- Create a new, blank database.
- Open a new module in Design view.
- On the Tools menu, click References.
- Add a reference to the Microsoft DAO 3.6 Object Library, and then click OK to close the References dialog box.
- Add the following code to the module:
Option Compare Database
Option Explicit
Sub OpenPasswordProtectedDB()
'Define as Static so the instance of Access
'doesn't close when the procedure ends.
Static acc As Access.Application
Dim db As DAO.Database
Dim strDbName As String
strDbName = "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"
Set acc = New Access.Application
acc.Visible = True
Set db = acc.DBEngine.OpenDatabase(strDbName, False, False, ";PWD=nwind")
acc.OpenCurrentDatabase strDbName
db.Close
Set db = Nothing
End Sub
- Run the OpenPasswordProtectedDB subroutine in the Immediate
window.
Note that the Northwind database opens in the new instance of
Microsoft Access without the password prompt.
Afterward, you may want
to remove the database password from the sample database Northwind.mdb. To do
so, follow these steps:
- Start Microsoft Access 2000.
- Open the sample database Northwind.mdb for exclusive use.
To open the database for exclusive use, click Northwind.mdb in the Open dialog box, click the arrow next to the Open button, and then click Open Exclusive.
- When prompted for the database password, type
nwind, and then click OK.
- On the Tools menu, point to Security, and then click Unset Database Password.
- When prompted for the database password, type
nwind, and then click OK.
- Close the database.
The database password is removed.
back to the top
REFERENCES
For additional information about opening a password
protected database with DAO, click the article number below to view the article
in the Microsoft Knowledge Base:
209953 ACC2000: How to Use the OpenDatabase Method to Open Password-Protected Databases
back to the top