Have a request for a topic in Code of the Week? Email us at firstname.lastname@example.org
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 email@example.com .
If you have any questions about using this class, let us know at firstname.lastname@example.org
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.
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" dbu.ResetDatabasePassword Exit Sub Handler: MsgBox Err.Description
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: http://www.codeoftheweek.com/membersonly/bi/0087.html