Visual Basic Code of the Week (COTW)
Issue #86
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.

Product Information

Check out for a great Internet performance monitor.

In this Issue

Password Protected Access databases from VB. The complete source code appears near the end of this issue. Be sure not to miss it!



One feature that has been requested many times lately is the ability to create and open password protected databases. This issue shows how to create a password protected Microsoft Access database from Visual Basic. You simply set a few properties and call the CreateDatabase method and it will automatically create a database that requires a password for opening. This is a great feature to keep Microsoft Access users from messing with your Visual Basic created Access databases.

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 . We will also show how to password protect an existing database. If you are interested in this topic email us at

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


Public DatabaseName As String

The fully qualified database name to be opened or created.

Public DatabasePassword As String

The password used to open or create the database.

Public Property Let SystemDatabase(sSystemDatabase As String)

The fully qualified path name to the workgroup information file. This file is called system.mdw by default. On a single user system it is often stored in the Windows system directory. In a network environment you probably want to put this file in a shared location for all users to access.

Public Property Get WorkspaceName() As String
Public Property Let WorkspaceName(sWorkspaceName As String)

The name of the workspace. This can be any string you would like. The default is DBPassword.

Public Property Get Username() As String
Public Property Let Username(sUsername As String)

The username to use when creating the workspace. This is typically Admin if you have not yet defined additional users in Microsoft Access. In an upcoming issue we will discuss how to add, edit and delete users.

Public Property Get Password() As String
Public Property Let Password(sPassword As String)

The password to use when creating the workspace. This is typically a blank string until you change it in Microsoft Access. In an upcoming issue we will discuss how to add, edit and delete users.

Public Property Get WorkspaceType() As WorkspaceTypeEnum
Public Property Let WorkspaceType(sWorkspaceType As WorkspaceTypeEnum)

The type of workspace to create. This class currently supports the dbUseJet workspace type. We will try to cover the dbUseODBC type in a future issue.


Public Sub CreateDatabase()

Creates a database specified by the properties set above. If the database already exists you will get an error raised back to the caller.

Public Function OpenDatabase() As Database

Opens a database specified by the properties set above. This function returns a database object you can use with most other objects in Visual Basic.


See specific details for each property and method.

Sample Usage

Below is an example of how this function works. It will create a database called e:\temp\password.mdb with a password of 'good'. It assumes this database does not already exist.

    Dim dbu As New cDBPassword
    Dim db As Database

    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\password.mdb"
    dbu.DatabasePassword = "good"

    ' create the database with a password

    ' open password protected database.
    Set db = dbu.OpenDatabase

    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