Password change by user

K

KevinT

Hi,

I have setup usernames and passwords in the database using the tools -
Security path.
To enhance security I have disabled the ByPass key (Shift key).
However, this means that I have to change their passwords for them etc.

What I would like is to have a Command Button on the startup page that would
act as a Shortcut to the Tools/Security Change password window.
Of course, they would only be able to change their password, i.e. CurrentUser.

TIA.

Kevin
 
G

Graham R Seach

Kevin,

'Change a user's password
Public Sub ChangeUserPassword(strUser As String, _
strOldPassword As String, strNewPassword As String)
Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)
Set usr = wrk.Users(strUser)

'Change the password
usr.NewPassword strOldPassword, strNewPassword

Set usr = Nothing
Set wrk = Nothing
End Sub

Call it using the following syntax:
ChangeUserPassword DBEngine(0).UserName, "oldpassword", "newpassword"

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Thanks Graham,

But where does it all go?
I'm learning as I go, but I've still a way to go.

Kevin,
Sydney also.
 
G

Graham R Seach

Kevin,

Place the code I gave you into a standard module.

I'd recommend creating a new form to allow the user to change their
password. Place 3 textboxes and 2 command buttons on this form.

The 3 textboxes are: txtCurrentPassword, txtNewPassword, and
txtConfirmNewPassword. Align them vertically in the listed order.

The 2 command buttons are: cmdChangePassword, and cmdCancel. Align them
horizontally at the bottom-right of the form.

Put code behind the cmdChangePassword button to (a) check that the contents
of txtNewPassword and txtConfirmNewPassword are identical, and if so (b)
actually change the password:
Private Sub cmdChangePassword_Click()
On Error Resume Next

If Me!txtNewPassword = Me!txtConfirmNewPassword Then
ChangeUserPassword DBEngine(0).UserName, _
Me!txtOldPassword, Me!txtNewPassword

If (Err <> 0) Then
MsgBox "Error " & Err.Number & vbCrLf & _
Err.Description, vbOkOnly+vbExclamation, _
"Could not change password"
End If
Else
MsgBox "The new passwords do not match."
End If
End Sub

Put code behind the cmdCancel button to close the form using the following
syntax:
Private Sub cmdCancel_Click()
DoCmd.Close acForm, Me.Name
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Thanks heaps Graham,

Just one question though.
Does this only allow the current user to change only their password?

Kevin
 
G

Graham R Seach

Kevin,

The ChangeUserPassword() procedure doesn't care who anyone is; it just
attempts to change the password based on the username provided. Creating a
form and calling ChangeUserPassword() using DBEngine(0).UserName ensures
that the current user can only change their own password. If you want to be
able to change anyone's password, you'll need to add another textbox for
username (txtUsername) and then modify the call to ChangeUserPassword() like
so:

ChangeUserPassword Me!txtUsername, _
Me!txtOldPassword, Me!txtNewPassword

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Thanks Graham.
Much appreciated.

Graham R Seach said:
Kevin,

The ChangeUserPassword() procedure doesn't care who anyone is; it just
attempts to change the password based on the username provided. Creating a
form and calling ChangeUserPassword() using DBEngine(0).UserName ensures
that the current user can only change their own password. If you want to be
able to change anyone's password, you'll need to add another textbox for
username (txtUsername) and then modify the call to ChangeUserPassword() like
so:

ChangeUserPassword Me!txtUsername, _
Me!txtOldPassword, Me!txtNewPassword

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Graham,

I created a test database and created some usernames to test the coding
before I put it into the actual database.

It came up with an error code saying it could not find the field
"txt!OldPassword".
I copied & pasted the coding from your post, so there isn't any typos.
I did notice, both in the module and cmdButton, that you have
"txt!CurrentPassword" and "txt!OldPassword". Is there a difference?
I did play around with it and changed Current to Old, but it then came up
with a "Null" error. I was trying to set a new password where there was no
password set prior.

Should I keep the "Current" or change all to "Old", and is there any
importance to what I name the Module?

Kevin
 
G

Graham R Seach

Kevin,
No you didn't! And yes there is!

I wrote "Me!txtOldPassword". You wrote "txt!OldPassword". These are two
completely different things. Copy the code from my 2nd posting *exactly* as
it is, then try again.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Graham,

Sorry, I mistyped the message I sent in the post.
However, I did not mistype the coding.
I copied and pasted it exactly.
I've deleted the test database and started again anyway.

The steps I've taken are as follows: (Note: The code you see below is copied
directly from the database)

I created a number of UserNames, some with passwords, some not, using the
User level security wizard.
I created a new form with the name "Form1",
In this I created 3 text boxes vertically aligned with the names, top to
bottom -
txtCurrentPassword,
txtNewPassword,
txtConfirmNewPassword.

I then created 2 cmd buttons named cmdChangePassword, and cmdCancel
respectively.

In the cmdChangePassword I pasted the following under the On Click
(selecting Code Builder as the option instead of Macro or Expression):

Private Sub cmdChangePassword_Click()
On Error Resume Next

If Me!txtNewPassword = Me!txtConfirmNewPassword Then
ChangeUserPassword DBEngine(0).UserName, _
Me!txtOldPassword, Me!txtNewPassword

If (Err <> 0) Then
MsgBox "Error " & Err.Number & vbCrLf & _
Err.Description, vbOKOnly + vbExclamation, _
"Could not change password"
End If
Else
MsgBox "The new passwords do not match."
End If

End Sub

I then added the appropiate code under the Cancel button.

I then created a new module with the name Module1.
In this Module I pasted the following:

'Change a user's password
Public Sub ChangeUserPassword(strUser As String, _
strOldPassword As String, strNewPassword As String)
Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)
Set usr = wrk.Users(strUser)

'Change the password
usr.NewPassword strOldPassword, strNewPassword

Set usr = Nothing
Set wrk = Nothing
End Sub

I then saved it all.

I then reopened it and opened Form1.
I entered the current and new passwords in each of the 3 boxes and clicked
the Change password button as above.
It gave me the error message 2465. "Microsoft Access can't find the field
'txtOldPassword' referred to in your expression".

Hope I done it properly.

Thanks,

Kevin
 
G

Graham R Seach

Kevin,

Aahhh, I see! It *was* my mistake!

Yes, rename "Me!txtOldPassword" to "Me!txtCurrentPassword", and all should
be well with the world.

Sorry for the confusion.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Thanks Graham.
All is well with the world.

Graham R Seach said:
Kevin,

Aahhh, I see! It *was* my mistake!

Yes, rename "Me!txtOldPassword" to "Me!txtCurrentPassword", and all should
be well with the world.

Sorry for the confusion.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
G

Graham R Seach

"...and the people of the small village of <enter village name> slept
soundly in their beds...until..." <fade to eerie music>

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Graham,

What do I need to add to the command button code to display a message saying
"Your password change was successful" (if it was successful) and also close
the window?

Kevin
 
G

Graham R Seach

Private Sub cmdChangePassword_Click()
On Error Resume Next

If Me!txtNewPassword = Me!txtConfirmNewPassword Then
ChangeUserPassword DBEngine(0).UserName, _
Me!txtCurrentPassword, Me!txtNewPassword

If (Err = 0) Then
MsgBox "You password was changed.", _
vbOkOnly+vbInformation, _
"Password change"
Else
MsgBox "Your password could not be changed." & _
vbCrLf & vbCrLf & _
Error " & Err.Number & _
vbCrLf & Err.Description, _
vbOkOnly+vbExclamation, _
"Password change"
End If
Else
MsgBox "The new passwords do not match."
End If
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

Thanks Graham.
Much appreciated.

Kevin

Graham R Seach said:
Private Sub cmdChangePassword_Click()
On Error Resume Next

If Me!txtNewPassword = Me!txtConfirmNewPassword Then
ChangeUserPassword DBEngine(0).UserName, _
Me!txtCurrentPassword, Me!txtNewPassword

If (Err = 0) Then
MsgBox "You password was changed.", _
vbOkOnly+vbInformation, _
"Password change"
Else
MsgBox "Your password could not be changed." & _
vbCrLf & vbCrLf & _
Error " & Err.Number & _
vbCrLf & Err.Description, _
vbOkOnly+vbExclamation, _
"Password change"
End If
Else
MsgBox "The new passwords do not match."
End If
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
G

Graham R Seach

Hi Kevin,

Oops, I forgot - you also wanted to close the form.

Private Sub cmdChangePassword_Click()
On Error Resume Next

If Me!txtNewPassword = Me!txtConfirmNewPassword Then
ChangeUserPassword DBEngine(0).UserName, _
Me!txtCurrentPassword, Me!txtNewPassword

If (Err = 0) Then
MsgBox "You password was changed.", _
vbOkOnly+vbInformation, _
"Password change"

DoCmd.Close acForm, Me.Name
Else
MsgBox "Your password could not be changed." & _
vbCrLf & vbCrLf & _
Error " & Err.Number & _
vbCrLf & Err.Description, _
vbOkOnly+vbExclamation, _
"Password change"
End If
Else
MsgBox "The new passwords do not match."
End If
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
K

KevinT

No problem Graham,
I managed to sort it out myself.
I feel I achieved something doing it, albeit a small thing.
By the way, I added it to the database proper late this arvo, and it worked
perfectly.
I logged on under different usernames with different security levels and all
was fine.
The exception may have been if the current password was null, but I got
around that by stating all users must have passwords or to contact me and
I'll reset their password the old fashioned way, setting "password" as their
password, which they can then change.

All is good with the world at last.

Thanks,

Kevin

ps I wish I was 30 years younger and had a lot more ability in this stuff,
its fascinating.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top