Changing password programatically

W

Westeral

I am trying to create a simple password manager database for users at my work
site so that they do not keep passwords taped to the screen or in their desk
drawer. The database will be standalone and have a password. When they first
open the database they will change their password. The problem is the code I
have to change it will not work. It runs into a permissions problem even
though I have set the default to open exclusively. The code for the button
and module it uses follow. What am I doing wrong? (I can send the entire
database if needed although it is not totally finished). The database will
be a standalone on each users computer. Thanks in advance.

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 "Your 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


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
 
A

Arvin Meyer [MVP]

Here's some ancient code that I remember being used in a database to change
the password. A menu option was used yo open a change password form.

The form had three fields: OldPassword, NewPassword and
VerifyNewPassword. It also has two buttons (the code for the click
events is shown below. To display stars rather than the password on
the screen set the Input Mask to Password

' Declarations:

Option Compare Database 'Use database order for string comparisons
Option Explicit

Dim OldPasswordVar As String
Dim NewPasswordVar As String
Dim VerifyVar As String

' Code for Click Event of OK button

Sub OKButton_Click ()

On Error GoTo Err_OKButton_Click


If IsNull(Me!OldPassword) Then
OldPasswordVar = ""
Else
OldPasswordVar = Me!OldPassword
End If

If IsNull(Me!newpassword) Then
MsgBox "Please enter a New Password"
Me!newpassword.SetFocus
Exit Sub
Else
NewPasswordVar = Me!newpassword
End If

If IsNull(Me!Verify) Then
MsgBox "Please verify New Password"
Me!Verify.SetFocus
Exit Sub
Else
VerifyVar = Me!Verify
End If

If NewPasswordVar = VerifyVar Then
DBEngine.Workspaces(0).Users(CurrentUser()).NewPassword
OldPasswordVar, NewPasswordVar
MsgBox "Password changed successfully"
DoCmd Close
Else
MsgBox "Please re-enter new password and verify"
Me!newpassword.SetFocus
Exit Sub
End If


Exit_OKButton_Click:
Exit Sub

Err_OKButton_Click:
Beep
If Err = 3001 Then
MsgBox "You have typed an invalid New Password." & _
vbCrLf & "The maximum length for a password is 14 characters."
ElseIf Err = 3033 Then
MsgBox "Incorrect Old Password. Please try again."
Else
MsgBox Error$ & Err
End If
Resume Exit_OKButton_Click

End Sub


' Code for Click Event of the Cancel Button:

Sub CancelButton_Click ()
DoCmd Close
End Sub

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com

Disclaimer: Any code or opinions are offered here as is. Some of that
code has been well tested for number of years. Some of it is untested
"aircode" typed directly into the post. Some may be code from other
authors. Some of the products recommended have been purchased and
used by the author. Others have been furnished by their manufacturers.
Still others have not been personally tested, but have been
recommended by others whom this author respects.

You can thank the FTC of the USA for making this disclaimer necessary.
 
W

Westeral

Thanks loads. This will work. Sorry for the late reply but I had decided
not to do the database but have changed my mind. Our Access program at work
(State of Michigan intranet), has security disabled so I cannot set passwords
or change passwords or even lock down the code. I am going to do the
database in Access 2010 and then implement with Runtime. The state internet
is going to start using Access 2003 this year so using Access 2010 runtime
will keep it secure enough for our intranet as nobody has Access 2010 to
access it otherwise. I know a password database is not the most secure but
it will be much better than the way our users currently keep their passwords,
on a piece of paper inside their desk drawer, on their calendar, etc. Again
thanks.
 

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