Working with Passwords
Many times, the administrative user needs to add, remove, or modify users' passwords. By using the user interface, you can modify only the password of the user currently logged in; by using code, however, you can modify any user's password, as long as you have administrative rights to do so.
Assigning Passwords to Users
The frmMaintainAll form enables the administrative user to assign a password to the user selected in the combo box. Listing 28.17 shows the code to assign a new password for a user.
Listing 28.17 Changing a User's Password
Private Sub cmdPassword_Click()
Dim boolSuccess As Boolean
If IsNull(Me.cboUserName.Value) Then
MsgBox "You Must Fill In User Name and Password Before Proceeding"
Else
boolSuccess = AssignPassword()
If boolSuccess Then
MsgBox "Password Successfully Changed"
Else
MsgBox "Password Not Changed"
End If
End If
End Sub
This routine ensures that a username has been entered and then calls the AssignPassword function, located in the frmMaintainAll form, as shown in Listing 28.18.
Listing 28.18 Using the AssignPassword Function to Change a User's Password
Function AssignPassword()
On Error GoTo AssignPassword_Err
Dim cat As ADOX.Catalog
Dim usr As ADOX.User
Set cat = New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
AssignPassword = True
'Use the ChangePassword method of the User object
'to change the password associated with the user
'selected in the combo box
Set usr = cat.Users(Me.cboUserName.Value)
usr.ChangePassword _
", Nz(Me.txtPassword.Value)
AssignPassword_Exit:
Set cat = Nothing
Exit Function
AssignPassword_Err:
MsgBox "Error # " & Err.Number & ": " & Err.Description
AssignPassword = False
Resume AssignPassword_Exit
End Function
The AssignPassword function points the User object at the user selected in the cboUserName combo box. It then uses the ChangePassword method of the User object to change the password associated with that user. The first parameter, the old password, is left blank intentionally. Members of the Admins group can modify anyone's password but their own without having to know the old password. The second parameter, the new password, is the value entered in the txtPassword text box. The Nz function sets the new password to a zero-length string if the administrative user did not supply a new password.
Listing Users Without Passwords
Many times, an administrative user simply wants to obtain a list of all users who do not have passwords. This list can be obtained quite easily by using VBA code and the ADOX library. Figure 28.4 shows the frmMaintainPasswords form, which is located in the CHAP28EX.MDB database.
Figure 28.4. This form enables administrative users to view users without passwords.

When the form is loaded, the list box uses the AddItem method of the listBox control to display a list of all users who do not have passwords. Listing 28.19 shows the code for the frmMaintainPasswords form.
Listing 28.19 Locating Users Without Passwords
Function ListUsers()
On Error GoTo ListUsers_Err
Dim cat As ADOX.Catalog
Dim cnn As ADODB.Connection
Dim usr As ADOX.User
Dim boolNoPass As Boolean
Set cat = New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
For Each usr In cat.Users
boolNoPass = True
Set cnn = New ADODB.Connection
cnn.Open CurrentProject.Connection, usr.Name, "
If boolNoPass Then
Me.lstUserName.AddItem usr.Name
End If
Next usr
ListUsers_Exit:
Set cat = Nothing
Set usr = Nothing
Exit Function
ListUsers_Err:
If Err.Number = -2147217843 Then
boolNoPass = False
Resume Next
Else
MsgBox "Error # " & Err.Number & ": " & Err.Description
Resume ListUsers_Exit
End If
End Function
The meat of the code is in the For…Each loop. The code loops through each user in the Users collection. It begins by setting the value of the boolNoPass flag to True. It creates a new catalog and attempts to log on to the new catalog by using the Name property of the current user object and a password that is a zero-length string. If an error occurs, the error-handling code sets the boolNoPass flag to False. The 2147217843 error means that the password was not valid, indicating that the user must have a password because the logon was not successful. If the logon was successful, the user must not have a password, and is therefore added to the list box.
Ensuring That Users Have Passwords
You might want to ensure that users who log on to your application have a password. You can accomplish this by using the code in Listing 28.20.
Listing 28.20 Code That Ensures Your Application's Users Have Passwords
Function AutoExec()
Dim cat As ADOX.Catalog
Dim usr As ADOX.User
Dim strPassword As String
Set cat = New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
Set usr = cat.Users(CurrentUser)
On Error Resume Next
usr.ChangePassword ", "
If Err.Number = 0 Then
strPassword = InputBox("You Must Enter a Password _
Before Proceeding", "Enter Password")
If strPassword = " Then
DoCmd.Quit
Else
usr.ChangePassword ", strPassword
End If
End If
AutoExec = True
End Function
The AutoExec function can be called from the startup form of your application. It points a User object variable to CurrentUser. It accomplishes this by using the return value from the CurrentUser function as the user to look at in the Users collection. The CurrentUser function returns a string containing the name of the current user.When an object variable is pointing at the correct user, the code attempts to set a new password for the user. When modifying the password of the current user, both the old password and the new password must be supplied to the ChangePassword method of the User object. If the old password is incorrect, an error occurs. This indicates that the user has a password and nothing special needs to happen. If no error occurs, you know that no password exists, so the user is prompted for a password. If the user does not supply a password, the application quits. Otherwise, a new password is assigned to the user.NOTE
![]() | You can find an example of the usefulness of this function in the basUtils module in the Chap28ExNoPass.mdb database located on your sample code CD-ROM. |