Visual Basic Code of the Week (COTW)
Issue #87
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp. Word, Excel and Visual Basic are trademarks of Microsoft Corp.


Have a request for a topic in Code of the Week? Email us at

In this Issue

More about password protected Microsoft Access databases from VB. This is an enhancement of issue number 86. The complete source code appears near the end of this issue. Be sure not to miss it!



In our last issue we discussed how to create a database with a password. This issue explains how to add a password to an existing database. It also shows a technique to determine if the database is opened by any other user. For the complete documentation of the properties in this class, be sure to review issue 86.

We had to modify the OpenDatabase method from issue 86, so be sure to check out the new definition. We have also added several new methods and functions to implement the password features described above.

In an upcoming issue we will show how to password protect certain queries and tables by using the Group and User features of Access. If you are interested in this topic, please email us at .

If you have any questions about using this class, let us know at


Public Function OpenDatabase(Optional bExclusive As Boolean = False, Optional bReadOnly As Boolean = False) As Database

Opens a database specified by the properties described in issue number 86. This function returns a database object you can use with most other objects in Visual Basic. The two optional parameter control how the database is opened. When bExclusive is True it means that only one user or task can open the database. When bReadOnly is True you can not make any changes to the database. Some operations perform much faster when the data is opened exclusively and/or read only.

An error will be raised if the database can not be opened in the mode specified.

Public Function HasPassword() As Boolean

Returns True if this database has a password attached to it.

Public Function IsClosed() As Boolean

Returns True if there are no other users currently accessing this database. It performs this task by attempting to open the database exclusively. If it succeeds in opening the database there can not be any other users accessing the database.

Public Sub SetDatabasePassword(sNewPassword As String)

Assigns a password to an existing Access database. Set the sNewPassword parameter to the password you would like the database to require to open. Upon successful assignment of a password it will update the DatabasePassword property with the new password. The DatabasePassword property must be set to the existing database password for this function to work correctly. If there is no password currently assigned to the database DatabasePassword should equal an empty string.

Public Sub ResetDatabasePassword()

Remove any password set by SetDatabasePassword. The DatabasePassword property must be set to the existing database password for this function to work correctly.


See specific details for each property and method.

Sample Usage

Below is an example of how this function works. It will add a password to the database called e:\temp\exists.mdb. It assumes this database already exists. It will then remove the password.

    Dim dbu As New cDBPassword

    On Error Goto Handler

    ' Point to the workgroup file.  If you are not sharing the database this is not
    ' necessary.
    dbu.SystemDatabase = "c:\windows\system\system.mdw"
    ' In the next week or so we will discuss how to add additional users to
    ' the system.
    dbu.Username = "admin"
    dbu.Password = ""

    ' Set the appropriate properties for the database name and password
    dbu.DatabaseName = "e:\temp\exists.mdb"
    dbu.DatabasePassword = ""

    ' assigns a new password 'newpass' to the database exists.mdb
    dbu.SetDatabasePassword "newpass"

    ' removes the password just assigned
    dbu.DatabasePassword = "newpass"
    Exit Sub

    MsgBox Err.Description

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945