Password error handling (1 Viewer)

hllary

Registered User.
Local time
Today, 06:33
Joined
Sep 23, 2019
Messages
80
I have a password protecting a form when it opens. That part works, the problem is I get a error if the user hits the cancel button or the wrong password is given.

I get a run-time error '2501': The OpenForm action was canceled. The msgbox is opening before the error message opens.

The code for the password is:

Code:
Private Sub Form_Open(Cancel As Integer)

  On Error GoTo Error_Handler
 
    If StrComp(InputBox("Please enter the Password."), Forms![Passwords_frm].[TxtbxPass1], 0) <> 0 Then
        Cancel = True
        MsgBox "Wrong password.", vbInformation Or vbOKOnly, "Operation cancelled"
    End If
 
Error_Handler_Exit:
   On Error Resume Next
   Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
          "Error Number: " & Err.Number & vbCrLf & _
          "Error Source: Form_Open" & vbCrLf & _
          "Error Description: " & Err.Description & _
          Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
         , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
    
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:33
Joined
Oct 29, 2018
Messages
21,357
Hi. I think the error is happening in the code that opens the form, so you'll probably need to handle it there.
 

Micron

AWF VIP
Local time
Today, 09:33
Joined
Oct 20, 2018
Messages
3,476
I think the error is happening in the code that opens the form, so you'll probably need to handle it there.
Exactly. In formA the code opens formB. FormB open is cancelled, and basically it reports back to formA that it failed (OK, in this case, canceled). The implication of not disclosing that to formA would be that code intends to interact with formB, but it cannot. The error message is preventive in a way. As suggested, you need an error handler in formA.
 

hllary

Registered User.
Local time
Today, 06:33
Joined
Sep 23, 2019
Messages
80
I placed an error handler on the FormA but now the FormB opens. How do I stop FormB from opening?

Is there an easier way to password protect a form?
 

Cronk

Registered User.
Local time
Tomorrow, 00:33
Joined
Jul 4, 2013
Messages
2,770
Do you get the prompt to enter a password as well as the "wrong password" message?

Maybe make the comparison simpler. Perhaps
Code:
if InputBox("Please enter the Password.") <> Forms![Passwords_frm].[TxtbxPass1] then
 

Micron

AWF VIP
Local time
Today, 09:33
Joined
Oct 20, 2018
Messages
3,476
swap the message box and cancel. Don't cancel a form opening then expect more code to execute. Can work sometimes, but it's not a logical progression.
Can we see the formA code, or at least the part that will handle 2501 error because of canceled formB opening? If the form opens but you don't get the error now, that is a different animal. That would indicate your input test is no longer working. Make sure that Forms![Passwords_frm].[TxtbxPass1] has the value you expect.

2nd question- other than preventing someone from having the ability to open the form in the first place, I'd say probably not. Trapping the error should be easier.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:33
Joined
Oct 29, 2018
Messages
21,357
I placed an error handler on the FormA but now the FormB opens. How do I stop FormB from opening?

Is there an easier way to password protect a form?
Okay, if you want easy, you might avoid using the Cancel argument. Instead, try doing a DoCmd.Close if the password is wrong.
 

hllary

Registered User.
Local time
Today, 06:33
Joined
Sep 23, 2019
Messages
80
I got rid of the Cancel argument and used closed.
Thank you
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:33
Joined
Oct 29, 2018
Messages
21,357
I got rid of the Cancel argument and used closed.
Thank you
Congratulations! Glad to hear you got it sorted out. We were all happy to assist. Good luck with your project.
 

Cronk

Registered User.
Local time
Tomorrow, 00:33
Joined
Jul 4, 2013
Messages
2,770
Something else must have been changed as well. I've never had a form not close after setting cancel = true in the open event.

@Micron, code will continue to run in the form open event after setting cancel = true. It's only when the true value is returned from the Open procedure that Access closes the form.
 

Users who are viewing this thread

Top Bottom