How to open a password-protected database through Automation in Access 2000 (235422)



The information in this article applies to:

  • Microsoft Access 2000

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

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.

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


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







Modification Type:MajorLast Reviewed:7/28/2004
Keywords:kbAutomation kbProgramming kbHOWTOmaster KB235422 kbAudITPro