Changing a User Password

User passwords are stored in the workgroup information file. To change a user's password from your VBA code, use the ADOX User object's ChangePassword method. This method takes as parameters the user's current password and the new password. If a user does not yet have a password, use an empty string (" ") for the user's current password.

The procedure in Hands-On 17-15 demonstrates how to change a password for the Admin user. Recall that Admin is the default user account that has a blank password. In an unsecured Access database, all users are automatically

Part II

logged on using the Admin account. When establishing user-level security you should start by changing the password for the Admin user. Changing an Admin password activates the Logon dialog box the next time you start Microsoft Access. Only users with a valid user name and password will be able to log onto the database. Although users are permitted to change their own passwords, only a user that belongs to the Admins account can clear a password that another user has forgotten.

© Hands-On 17-15: Changing a User Password

While this hands-on uses the sample database that you prepared in Chapter 1 of this book, you can use any unsecured Access database for this password changing exercise.

1. Insert a new module and, in the module's Code window, enter the Change_UserPassword procedure as shown below.

Sub Change_UserPassword() Dim cat As ADOX.Catalog Dim strDB As String Dim strSysDb As String

On Error GoTo ErrorHandle strDB = CurrentProject.Path & "\Acc2003_Chap01.mdb" ' change the path to use the default workgroup information file ' on your computer strSysDb = "C:\Documents and Settings\JKorol\" & _

"Application Data\Microsoft\Access\System.mdw"

' Open the catalog, specifying the system database to use Set cat = New ADOX.Catalog With cat

.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" &_ "Data Source='" & strDB & "';" & _ "Jet OLEDB:System Database='" & strSysDb & "';" & _ "User Id=Admin;Password=;"

' Change the password for the Admin user .Users("Admin").ChangePassword " ", "secret" End With

ExitHere:

Set cat = Nothing Exit Sub ErrorHandle:

MsgBox Err.Description GoTo ExitHere End Sub

Creating and Manipulating Databases with ADO

Figure 17-24: When you open the Acc2003_ Chap01.mdb database after running the procedure in Hands-On 17-15, a Logon dialog will appear. Enter Admin in the Name text box and secret for the Admin password.

2. Remove the Admin password by choosing Tools | Security. Click the Change Logon Password tab and type the old password (secret). Click the Apply button, and click OK to exit the window (Figure 17-25).

Figure 17-25: You can remove the Admin password manually via the Change Logon Password tab in the User and Group Accounts window, or by modifying the VBA code shown in Hands-On 17-15.

Creating and Manipulating Databases with ADO

3. After removing the Admin password set in Hands-On 17-15, reopen the Acc2003_Chap01.mdb file. The database should open without prompting you to enter a password.

0 0

Post a comment