Exit Password Prompt Option

  • Thread starter Kyle Friesen via AccessMonster.com
  • Start date
K

Kyle Friesen via AccessMonster.com

Hello,

I have a password protected form that is working great thanks Access
Monster and MSN help. However, one missing piece that I tried to figure out
is that I want to add a exit option to the password form. Basically for
users that didn't intend on opening the password protected form.

here is the code I have for the Null value notification. Can I add some
sort of If IsNull then CloseForm line instead of the "try again"?

Any sort of attempt brings up "Invalid use of Null" error message that may
confuse users.

****
Private Sub CheckPassword_Click()
If IsNull(Forms!frmPassword!Text0.Value) Then
MsgBox "You Must Enter A Password. Try Again."
Me!Text0.SetFocus

Else
MyPassword = Me!Text0.Value
DoCmd.Close acForm, "frmPassword"
End If
End Sub
****

Thanks!
 
J

Jeff Conrad

I have a password protected form that is working great thanks Access
Monster and MSN help. However, one missing piece that I tried to figure out
is that I want to add a exit option to the password form. Basically for
users that didn't intend on opening the password protected form.

here is the code I have for the Null value notification. Can I add some
sort of If IsNull then CloseForm line instead of the "try again"?

Any sort of attempt brings up "Invalid use of Null" error message that may
confuse users.

****
Private Sub CheckPassword_Click()
If IsNull(Forms!frmPassword!Text0.Value) Then
MsgBox "You Must Enter A Password. Try Again."
Me!Text0.SetFocus

Else
MyPassword = Me!Text0.Value
DoCmd.Close acForm, "frmPassword"
End If
End Sub
****

Before I offer some suggestions, can you first verify something for me?
Is your form and code based on these KB articles?

http://support.microsoft.com/?id=209871
http://support.microsoft.com/?id=179371

If so, have you followed them to the letter or have you made modifications?
 
K

Kyle Friesen via AccessMonster.com

yes, exactly those articles.

I have a main form that everyone may access and at the bottom left corner
of the main form a open frm command button that executes the frmpassword
procedure (restricted form).

I want to add an exit button option on the frmpassword.

thanks, Jeff.

Kyle
 
K

Kyle Friesen via AccessMonster.com

jeff, I have followed those articles exactly, just want to add a feature
which allows users to exit the frmpassword. thanks.
 
K

Kyle Friesen via AccessMonster.com

Jeff,
yes, I would like to close the form without a password error message and I
have not made any changes to the on open procedure. thanks for your help.
 
J

Jeff Conrad

yes, exactly those articles.

I have a main form that everyone may access and at the bottom left corner
of the main form a open frm command button that executes the frmpassword
procedure (restricted form).

I want to add an exit button option on the frmpassword.

thanks, Jeff.

I just added a new command button onto frmPassword called
cmdExit with the following code in the Click event:

Private Sub cmdExit_Click()
DoCmd.Close acForm, "frmPassword"
End Sub

I put a Caption of Cancel, but you can use whatever you would like.

Hitting the Exit button will, however, still cause the calling form
to pop up the message box about incorrect password. Everything
worked just fine in my test.

If you would like to not show the "Incorrect Password" message box
when a user presses the Exit button on the frmPassword then let
me know. I have a solution for that as well. I would need to know
if you have made ANY changes to the Form_Open event code.
 
J

Jeff Conrad

Jeff,
yes, I would like to close the form without a password error message and I
have not made any changes to the on open procedure. thanks for your help.

Okie Dokie.

Follow these steps:

1. Make a couple backup copies of your database first

2. Open the frmPassword and change the Exit command button
Click event code to this:

Private Sub cmdExit_Click()
MyPassword = "ExitButton"
DoCmd.Close acForm, "frmPassword"
End Sub

3. Save and close that form

4. Open one of the forms that you are password protecting.
Go to the code window behind this form and look for the Open
event code. It should be exactly like the KB article code.
Look for this area of the code:

On Error GoTo Error_Handler
' Prompt the user for the Password.
DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
Hold = MyPassword

' Open the table that contains the password.
Set db = CurrentDb
....yadda....yadda....

In between those two areas we will add a few more lines of code.
It should now look like this:

On Error GoTo Error_Handler
' Prompt the user for the Password.
DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
Hold = MyPassword

' User pressed exit button so skip everything else
If Hold = "ExitButton" Then
Cancel = True
Exit Sub
End If

' Open the table that contains the password.
Set db = CurrentDb
....yadda....yadda....

What this will do is tell Access to cancel opening the protected form,
but not display anything since the rest of the code procedure is just
skipped.

You will need to enter the same code in the same spot for any other
forms or reports you are password protecting. Also, do NOT use
ExitButton as a password for any of the forms and reports.

Everything works fine in my tests.
Hope that helps,
 
K

Kyle Friesen via AccessMonster.com

great! thanks. one more quick one for you, Jeff.

Now I am getting a VB Error "The OpenForm action was canceled".

The form being the password protected object. Any ideas on how to elimate
that error message too?
 
K

Kyle Friesen via AccessMonster.com

Jeff, I changed the command button's on click from an event procedure to a
macro and turned the warnings off.

everything works fine now, thanks for your help.

Kyle
 
J

Jeff Conrad

great! thanks. one more quick one for you, Jeff.

Now I am getting a VB Error "The OpenForm action was canceled".

The form being the password protected object. Any ideas on how to elimate
that error message too?

Yep, easy to fix.

Error 2501 is the one you are encountering and it is the standard Access
message for this type of thing. The error is actually occurring on the main
form, not the protected one or frmPassword. You said you had a main
form that you users clicked to open the protected form. That main form
is the one you need to open in Design View and go to the code for
the click event of the button that opens the protected form.

We need to add some error handling to this click event. Most likely there
probably already is some error handling code. You just need to ignore
Error 2501. You will have to modify this example code for your situation:

Private Sub cmdOpenForm_Click()
On Error GoTo ErrorPoint

DoCmd.OpenForm "ProtectedFormNameHere"

ExitPoint:
Exit Sub

ErrorPoint:
If err.Number <> 2501 Then
' Unexpected Error
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & err.Number _
& vbNewLine & "Error Description: " & err.Description _
, vbExclamation, "Unexpected Error"
End If
Resume ExitPoint

End Sub

If you still have problems adapting this into your code, just post
all the code behind the click event for that main form button and
I will make the modifications for you.
 
J

Jeff Conrad

Jeff, I changed the command button's on click from an event procedure to a
macro and turned the warnings off.

everything works fine now, thanks for your help.

While the macro will work Kyle, I would not recommend it.
The macro could by chance encounter other error messages
which would cause Access to cough up a "Halt" hair ball.
I would suggest just trapping and ignoring the error as I
outlined in my other post.
 

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