On NoData event

MICHELE

Registered User.
Local time
Today, 22:10
Joined
Jul 6, 2000
Messages
117
I have several reports that print at one time . I would like to only print them if they have data on them. I've gone to the NoData event on the report and put Cancel = True. It works except it gives a message that it's cancelling the open report. How can I stop this message? I think it's something from the control on the form because if I try to open the report without using the control on the form I don't get the message.
 
What I do is base my reports on a query and then on the onclick event of the button to print the report I check to see if there is any data for the report:

If IsNull (DLookup("IDfieldname", "qryname", "IDfieldname") then
MsgBox "No data to print"

Else

DoCmd.OpenReport "rptName"

Exit Sub
End If

You can stack all the different report names so that if goes through each one looking to see if there is data or not before opening the report.

There might be a neater way to do it but this works well for me.
 
Change Cacel = True to DoCmd.Close
 
DBL-
Thanks, I've gotten this to work well. But how can I make it work without a message box that the user has to push "OK" everytime one of the reports has not data. I would like the reports without data to be skipped.
 
Change it to

If IsNull (DLookup("IDfieldname", "qryname", "IDfieldname") then
Exit Sub

Else

DoCmd.OpenReport "rptName"

Exit Sub
End If

So the code will just move to the next report if that one has no data without opening the report at all.

HTH
 
Just in case anyone is interested. I have the answer to Michele's question without having to use the DLookup function. (I was having the exact same problem as she was.) In the NoData event of the report, enter the following:

MsgBox "Whatever you want to say."
DoCmd.CancelEvent
SendKeys "{ENTER}"

The SendKeys function will send an Enter keystroke. That way, when the message comes up that says it's canceling the report, it will send the Enter key, and the message will go away without the user having to do anything. It all happens so fast, that you can't even tell that a message popped up.
 
This is an error caused because the button on the form cannot perform its task, therefore catch the error in the button code and deal with it there suchas:

Private Sub Whatever_Click()

On Error Goto Whatever_Click_Error

Docmd.OpenReport etc...etc..

Whatever_Click_Exit:
Exit Sub

Whatever_Click_Error:
if Error = 2501 'I think
Else
Msgbox Err.Description & vbCrLf & Err.Number
End if
End Sub

You do not HAVE to display an error message - you are getting the default for the error type.

Ian
 
Actually, I tried that very same thing that you have suggested based on messages in other threads, but it didn't work. I was not running the report from a form (so I guess my problem wasn't EXACTLY like Michele's, but I was getting the same error message when I put in the same code she was). I was calling a function from a command that I put in a custom menu. I tried error trapping in the function, but it didn't work.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom