Full Version: DoCmd.OpenReport error on no data
UtterAccess Forums > Microsoft® Access > Access Forms
I'm using the AfterUpdate event on a combo box in a form to open a report (using the combo box to establish my filter for the report). The report has some code in the On No Data event to pop a message box that says "No Data to Report for that Timeframe" and Cancel=True. Trouble is that because I'm using the DoCmd.OpenReport from the form, I still get an error message saying the OpenReport was cancelled. Any way to suppress that?
Rather then use the On No Data Event, I would use the OnActivate event with the following code:
Dim strMsg As String
Dim strTitle As String
strMsg = "No Records Returned"
strTitle = "No Records Returned"

If Me.Report.HasData Then
    Exit Sub
    MsgBox strMsg, vbOKOnly + vbExclamation, strTitle
    DoCmd.close acReport, Me.Name
End If

On your form where you are opening the report use:
DoCmd.OpenReport RptName, acViewPreview

Another approach is to continue to use the OnNoData event (which is my preference sad.gif ) ... and just suppress the error prior to the DoCmd.OpenReport ...
On Error Resume Next
DoCmd.OpenReport .....
If the DoCmd.OpenReport is NOT the last line of code before the End Sub / Exit Sub, then I reset the error handler ...
On Error Goto 0 (Turns OFF error handling)
... Or ...
On Error Goto ErrorHandlerLabel
Thanks for that approach as well, good for future reference.
You're welcome! ....
One BIG important note ... the OnActivate event DOES NOT FIRE if you print directly to the printer ... so ... if you do NOT Preview the report, the OnActivate will NOT fire .... thus my reason for sticking with the OnNoData.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.