Aviod open records with no records

richardn

Registered User.
Local time
Today, 12:46
Joined
Nov 6, 2001
Messages
49
I am using "DoCmd.OpenReport strMyRepName, acViewPreview, strMyFilter" command to open a report in a preview mode.
How can I avoid to open it if there are no records?
I tried to use the NoData event, but after this event occures the command DoCmd.OpenReport ... , and I get an error.
Please help,
Regards,
Richard
 
error 2501 isn't really an error, it's just an advice that the report didn't open.

in your button where you have the docmd.openreport command, do the following

on error goto fail
"DoCmd.OpenReport strMyRepName, acViewPreview, strMyFilter"

exithere:
exit sub

fail: - there was an error
call msgbox("Error: "&err.number & " Desc: " & err.description)
resume exithere:

this will identify the error as error 2501 (other errors MIGHT be significant)
so in the fail you can change this to

fail: - there was an error
select case err
case 2501: 'do nothing - this error is OK, or popup a message box saying there is no data (you may already have that in your report)
case else
call msgbox("Error: "&err.number & " Desc: " & err.description)
end select
resume exithere:
 
Try this:

If dcount("*","ReportSourceQuery")>0 then
docmd.openReport "ReportName", acViewPreview
end if

If the ReportSource Query have no records then the Report will not be opened at all.
 
apr pillai... that works really well. I have always used the on no data event, and couldn't find the syntax, so did a search on the board and saw your post. I used your idea and it is GREAT!
Thanks,
Dave
 
apr pillai... that works really well. I have always used the on no data event, and couldn't find the syntax, so did a search on the board and saw your post. I used your idea and it is GREAT!
Thanks,
Dave

Just a quick note -

Gemma's use of the error handler is actually more efficient than using a DCount. For most databases you won't notice a difference but on very large databases the difference can be very noticeable.
 

Users who are viewing this thread

Back
Top Bottom