Is there a way to cancel the form from Loading if certian condition is not met.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, I want to know of a way to cancel the form from Loading if certian condition is not met.

Code:
[/FONT]
[FONT=Courier New]Private Sub Form_Load()[/FONT]
[FONT=Courier New]if certian condition is met in table then
'open it
else 
 Cancel being opened
end if
End Sub

Is there anything like that?
Thanks for helping!

 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I tried messing around with Cancel = True but that didn't work. What about having the If...Then statement in whatever procedure that you are using to open the form? Have it check before the form even tries to load, is that an option?
 
Upvote 0
You need to check in the OPEN event of the form so you can use

Cancel = True

However, the recordset is not loaded yet so you would need to use another method of checking (like a DCount to see if there are records). You can actually do this BEFORE you even attempt to open the form and ONLY open the form if there are records which match the criteria. Then you don't need to cancel the open because you never opened it to begin with.
 
Upvote 0
Thanks again Bob for quick answer, this is connection to the "Public variable" 'm trigerring on my startup form 'm opening...if AccessX <> true then i want to cancel it from being opened....

I used this and works perfectly so far!!!:)


May I also ask what's the difference between close,load
and also Close and unload...?
Kind of confusing now:confused:...

Thanks again.
Code:
[/FONT]
[FONT=Courier New]Option Compare Database
Private Sub Form_Open(Cancel As Integer)
If AccessX = False Then
Cancel = True
End If
End Sub

You need to check in the OPEN event of the form so you can use

Cancel = True

However, the recordset is not loaded yet so you would need to use another method of checking (like a DCount to see if there are records). You can actually do this BEFORE you even attempt to open the form and ONLY open the form if there are records which match the criteria. Then you don't need to cancel the open because you never opened it to begin with.

 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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