View Full Version : Quiery about "On No Data"


Big Pat
03-13-2008, 09:52 AM
Hi,

I’m very much an Access amateur and I don't follow much code, but I know enough to have put this into my report

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No matching data for selected report."
DoCmd.CancelEvent
End Sub

... and this works OK.

But then I get another message saying "The OpenReport action was cancelled." Is there a way to turn this off? I tried

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No matching data for selected report."
DoCmd.SetWarnings False
DoCmd.CancelEvent
DoCmd.SetWarnings True
End Sub

...but this seems to have no effect. I suppose that second message isn't a warning, but "information". Is there some code I can add in that will stop this second message?

Grateful for any pointers!

pbaldy
03-13-2008, 09:59 AM
You have to trap for error 2501 in the code that opened the report. Searching on that should turn up the code if you're unfamiliar with error trapping.

gemma-the-husky
03-13-2008, 10:01 AM
instead of yuor code do this to cancel the form open

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No matching data for selected report."
cancel = vbcancel
End Sub

BUT how are you opening the report - if you are calling it from code you will get a form not opening error 2501), so you need to trap and dismiss it

on error goto fail
docmd.openreport ("myreport")
exithere:
exit sub

fail:
if err=2501 then resume exithere
msgbox(err.description)
resume exithere

Big Pat
03-13-2008, 10:06 AM
Thanks both for the assistance. I have HEARD of error-trapping and I KINDA know what it means so I guess this is the perfect opportunity for me to go read up on it. Just knowing what to search for is maybe half the battle.

My report opens from a button on a form. I used the wizard to create the button, telling it I wanted it to open that specific report in preview mode. I couldn't have coded this from scratch!

gemma-the-husky
03-13-2008, 10:55 AM
no but once youve done it a few times, and followed whats going on, it will become a lot easier