Link to home
Create AccountLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

Close form if condition is true

Hi Experts,

I need to do the following before the user starts using a form

If x = y Then
   MsgBox "You Can't use this form"
   Unload Me
End If

Open in new window

What would be the event I use here?  I Can't use Form_Load as i can't unload from here

Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
OnOpen has a Cancel event...

Private Sub Form_Open(Cancel as Integer)

if x = y then
   Msgbox "Your message goes here"
   Cancel = True
end if

End Sub
in your Form_Load event try

If x = y Then
   MsgBox "You Can't use this form"
   DoCmd.Close acForm, Me.Name
End If
Duplicate post ... Cap is correct.

You can also do this from the calling code...

If x = y then
   Msgbox "Your message goes here"
   DoCmd.OpenForm "form name", acNormal
End If
Why not check the condition before you try to open the form since I assume that x and y are not values on the form itself? Even if they are it's easy to create a Private Property(s) on the form that can be queried before the form is opened and without causing the form to open.