Microsoft KB Archive/235422

= How to open a password-protected database through Automation in Access 2000 =

Article ID: 235422

Article Last Modified on 7/28/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q235422





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
 * Step-by-Step Example

REFERENCES



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.</li> Click Cancel to prevent the database from opening.</li> Create a new, blank database.</li> Open a new module in Design view.</li> On the Tools menu, click References.</li> Add a reference to the Microsoft DAO 3.6 Object Library, and then click OK to close the References dialog box.</li>  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 </li> Run the OpenPasswordProtectedDB subroutine in the Immediate window.</li></ol>

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:
 * 1) Start Microsoft Access 2000.
 * 2) 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.
 * 3) When prompted for the database password, type nwind, and then click OK.
 * 4) On the Tools menu, point to Security, and then click Unset Database Password.
 * 5) When prompted for the database password, type nwind, and then click OK.
 * 6) Close the database.

The database password is removed.

back to the top

<div class="references_section">