users set their own password

A

AccessIM

I borrowed the following code for creating a form that the user's can set
their own password on a newly secured database. I have many users set up
with no password and would like to give them the ability to change it
themselves but when I test the form I get an "Error 94 Invalid use of Null"
message.

Here is the code on the form in the On_Click event of a button:

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

And here is the code in the Module:

'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 read threads about SetPassword but didn't see any supporting code samples.
Could someone please tell me where I am going wrong? Thank you so much.
 
T

tina

have you stepped through the code to see exactly what line is triggering the
error? comment out the On Error Resume Next line, first.

hth
 
A

AccessIM

Hi Tina-

I correct the On Error line and found the code is stopping at the following
line:

ChangeUserPassword DBEngine(0).UserName, _
Me!txtCurrentPassword, Me!txtNewPassword

I have been reading up on this error but have not found a solution. I think
it is safe to assume the error is appearing because, at this point, all
passwords are blank since i just set the users up. If I set a password for a
user and use this form and code, it changes the password and everything works
great. I just need to know how to change the code to accept the initial null
value in the txtCurrentPassword field.

Any suggestions?
 
C

Chris O'C via AccessMonster.com

You're passing a null value as a parameter to a data type (string) that can't
accept nulls. Use the nz function to assign a zero length string to the
value passed to strOldPassword when the value is null.

Chris
 
T

tina

as Chris said, the problem is probably the Null value in txtCurrentPassword.
try

ChangeUserPassword DBEngine(0).UserName, _
Nz(Me!txtCurrentPassword, ""), Me!txtNewPassword

hth
 

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