How do I allow my users to change their passwords in Access?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using Access 2002. Do I have to build a form with VB behind it to allow the
users of my system the ability to change their own passwords? Can I 'borrow'
an example from someone unless I'm missing an obvious solution?
Thanks, Greg
 
I have a form with 3 text boxes (txtOld, txtNew, txtVerify) and 2 buttons
(cmdCancel, cmdChange). here's all the code behind the form:

Option Compare Database
Option Explicit

Private Sub cmdCancel_Click()
DoCmd.Close
End Sub

Private Sub cmdChange_Click()

Dim strOld As String
Dim strNew As String
Dim strVerify As String

strOld = Nz(txtOld, "")
strNew = Nz(txtNew, "")
strVerify = Nz(txtVerify, "")

If ChangePassword(strOld, strNew, strVerify) = True Then
If strNew = "" Then
MsgBox "Your password has been cleared.", vbInformation, _
"Change password " & CurrentUser
Else
MsgBox "Your password has changed.", vbInformation, _
"Change password " & CurrentUser
End If
txtOld = Null
txtNew = Null
txtVerify = Null
End If

End Sub

Private Function ChangePassword(strOld As String, _
strNew As String, _
strVerify As String) As Boolean
On Error GoTo ErrorLine

Dim wks As Workspace
Dim rstUser As Recordset
Dim strSQL As String
'Check if new password matches with verification
If strNew <> strVerify Then
MsgBox "Your new password and the verification of your new password " & _
"do not match." & vbCrLf & vbCrLf & "Please try again.", _
vbExclamation, "Change Password " & CurrentUser
End If
'Change password
Set wks = DBEngine(0)
wks.Users(CurrentUser).NewPassword strOld, strNew
'Add new password in table
strSQL = "UPDATE tblUsers " & _
"SET tblUsers.UserCurrentPassword = " & Chr(34) & strNew & Chr(34) & ", "
& _
"tblUsers.UserPreviousPassword = " & Chr(34) & strOld & Chr(34) & " " & _
"WHERE (((tblUsers.UserLogin)=" & Chr(34) & CurrentUser & Chr(34) & "));"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

ChangePassword = Tru
'---------------------------------------------------------------------------------------
FinalLine:
Exit Functio
'---------------------------------------------------------------------------------------
ErrorLine:
MsgBox "Password cannot be changed. Please make sure your old password is
" & _
"correct.", vbExclamation, "Change Password " & CurrentUser
Resume FinalLine

End Function

Private Sub Form_Load()
Me.lblTitle.Caption = "Change Password " & UCase(CurrentUser)
End Sub
 
Thanks Nova,
I get an error on the following:
Private Sub Form_Load()
Me.lblTitle.Caption = "Change Password " & UCase(CurrentUser)
End Sub
Does this create the label dynamically? Help............Greg
 
sorry for the late reply, I don't frequently read these discussion groups.
Maybe you already sorted it out, but it maybe has to do with a reference file
yo have't loaded and that includes the property 'CurrentUser'. Or otherwise,
you're not properly logged in to your own database (opened it with shift
key?) That line that causes the error is only to display the user's login ID
 
I know that this post is 5 months old.. but i found it useful however I did
find some major errors in the code..

first of all to fix the caption problem.. just remove the reference
lblTitle if you want this to be the caption of your form .. or...

Private Sub Form_Load()
Me.Caption = "Change Password " & UCase(CurrentUser)
End Sub

Change the name of your Title label for your form (text at the top
describing what you're doing) to a textbox lblTitle and the code will work
fine with these modifications.. next...

Change the textbox to have the following properties
Enabeled = No
Locked = Yes
Back Style = transparent
Border = Solid
Or whatever you want

Private Sub Form_Load()
Me.lblTitle = "Change Password " & UCase(CurrentUser)
End Sub

The other adjustment that needs to be made in the code.. is more of a major
flaw. Even if txtNew and txtVerify do not match.. the User password will
still be changed. This can be fixed by changing the "End If" to "Else" and
adding the "End If" to the bottom of the code as follows. I have also
commented out the addition of the new user to a table called tblUsers as I
used the User-Level-Security Wizard.

Private Function ChangePassword(strOld As String, _
strNew As String, _
strVerify As String) As Boolean
On Error GoTo ErrorLine

Dim wks As Workspace
Dim rstUser As Recordset
Dim strSQL As String
'Check if new password matches with verification
If strNew <> strVerify Then
MsgBox "abcYour new password and the verification of your new password "
& _
"do not match." & vbCrLf & vbCrLf & "Please try again.", _
vbExclamation, "Change Password " & CurrentUser
Else
'Change password
Set wks = DBEngine(0)
wks.Users(CurrentUser).NewPassword strOld, strNew
'Add new password in table
' strSQL = "UPDATE tblUsers " & _
' "SET tblUsers.UserCurrentPassword = " & Chr(34) & strNew & Chr(34) &
", " & _
' "tblUsers.UserPreviousPassword = " & Chr(34) & strOld & Chr(34) & " "
& _
' "WHERE (((tblUsers.UserLogin)=" & Chr(34) & CurrentUser & Chr(34) &
"));"
' DoCmd.SetWarnings False
' DoCmd.RunSQL strSQL
' DoCmd.SetWarnings True

ChangePassword = True
End I
'---------------------------------------------------------------------------------------
FinalLine:
Exit Functio
'---------------------------------------------------------------------------------------
ErrorLine:
MsgBox "Password cannot be changed. Please make sure your old password is
" & _
"correct.", vbExclamation, "Change Password " & CurrentUser
Resume FinalLine

End Function


Maybe this will be useful for anyone who reads it.. this is a great solution
to the password change.

Cheers,

Pip'n
 
Hm, if someone gave me a "change password" form that //stored my
password in a table//, I would not be a happy chappy! :-)

TC
 
ok 2 years later but I found this post and it works perfectly, I added your
changes because I also used the security wizard. Awesome. Very simple.
 
Back
Top