ACC2000: How to Open a Password-Protected Database Through Automation
ID: Q235422
|
The information in this article applies to:
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies only to a Microsoft Access database (.mdb).
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.
MORE INFORMATION
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.
Step-by-Step Example
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.
- 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.
Afterwards, 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.
Additional query words:
inf
Keywords : kbdta
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
|