ACC2000: How to Open a Password-Protected Database Through Automation

ID: Q235422


The information in this article applies to:
  • Microsoft Access 2000

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.

  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. On the Tools menu, point to Security, and then click Set Database Password.


  4. Type nwind in the Password and Verify boxes, and then click OK.


  5. Close the sample database Northwind.mdb.


  6. Open the sample database Northwind.mdb to verify that you receive a prompt to enter the database prompt.


  7. Click Cancel to prevent the database from opening.


  8. Create a new, blank database.


  9. Open a new module in Design view.


  10. On the Tools menu, click References.


  11. Add a reference to the Microsoft DAO 3.6 Object Library, and then click OK to close the References dialog box.


  12. Add the following code to the module:


  13. 
    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 
  14. 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:
  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.

Additional query words: inf

Keywords : kbdta
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 29, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.