Form Load VBA Help

lorenambrose

Active Member
Joined
Sep 17, 2008
Messages
265
I have a form that on loading prompts for a password.

How do I modify this code to close or NOT LOAD the form if the user inputs the wrong password. Right now if the password is incorrect the "Authorization Denied" msg box appears and I would like the form to NOT OPEN when they select OK. Hope someone can help.


Private Sub Form_Load()

If InputBox("Enter Password") = "qazz" Then
DoCmd.OpenForm "Edit an ER"
Else
MsgBox "Authorization Denied", vbOKOnly

End If

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Immediately after the MsgBox, before the End If, insert:-
Code:
DoCmd.Close acForm, Me.Name
 
Upvote 0
Instead of using the form's Load event you can use the OPEN event which is cancelable:

Rich (BB code):
Private Sub Form_Open(Cancel As Integer)
 
If InputBox("Enter Password") = "qazz" Then
   DoCmd.OpenForm "Edit an ER"
Else
   MsgBox "Authorization Denied", vbOKOnly
   Cancel = True
End If
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,536
Messages
6,179,402
Members
452,909
Latest member
VickiS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top