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