Cancel form load (1 Viewer)

RossG

Registered User.
Local time
Today, 21:01
Joined
Apr 18, 2001
Messages
21
Hi

I have a form that is opened using a query as the filter.

In the OnLoad event I check the recordsetclone.recordcount and if it is zero then pop up a message box to the effect of 'No records found'.

Is there a way to then cancel the form load after the user clicks the message box OK button?

TIA
 

DALeffler

Registered Perpetrator
Local time
Today, 14:01
Joined
Dec 5, 2000
Messages
263
Try, "Cancel = True"

Private Sub Form_Open(Cancel As Integer)
.....'Get records
if recordsetclone.recordcount = 0 then
Msg = "No Records Found"
Title = "Your Title of MsgBox"
Style = vbOKOnly
MsgBox Msg, Style, Title
Cancel = True
end if
end sub
 

RossG

Registered User.
Local time
Today, 21:01
Joined
Apr 18, 2001
Messages
21
This works for me....

but I keep getting a Runtime error 2051.

Have tried to inhibit the warning message by setting warnings off, also trying to trap that specific error number, but can't stop it.

Any suggestions on how to stop the warning?

Thanks
 
R

Rich

Guest
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
if recordsetclone.recordcount = 0 then
Msg = "No Records Found"
Title = "Your Title of MsgBox"
Style = vbOKOnly
MsgBox Msg, Style, Title
Cancel = True
end if
end sub
HTH
 

RossG

Registered User.
Local time
Today, 21:01
Joined
Apr 18, 2001
Messages
21
Nope, that still isn't working :-(

After clicking the OK button on the message box I get the Error 2501 and warning saying the OpenForm method was cancelled.

Then have the option of debugging or End. The latter gets rid of the warning and everything is then OK. This is the warning I want to get rid of. Have tried sending Alt+E using SendKeys to close the warning message but that didn't work either.
 
R

Rich

Guest
Try:
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
if recordsetclone.recordcount = 0 then
Cancel = True
Msg = "No Records Found"
Title = "Your Title of MsgBox"
Style = vbOKOnly
MsgBox Msg, Style, Title
end if
end sub
 

RossG

Registered User.
Local time
Today, 21:01
Joined
Apr 18, 2001
Messages
21
YAY....Success!!

Glad to say it's now fixed!

It finally dawned on me the error message was not being generated within the Load/Open events of the new form, but rather in the command button click event on the FIRST page (which contained the DoCmd.OpenForm code).

In the error handling of this form I'm specifically trapping the Err.Number 2501 and using the Resume Next code there, while leaving all other errors to be handled 'normally'.

Dunno if it is good coding practice, but sure as heck works!

Thanks for your help guys!
 

Users who are viewing this thread

Top Bottom