Full Version: DoCmd.OpenReport error on no data
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
DerekC
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?
dashiellx2000
Rather then use the On No Data Event, I would use the OnActivate event with the following code:

CODE
Dim strMsg As String
Dim strTitle As String
strMsg = "No Records Returned"
strTitle = "No Records Returned"

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


On your form where you are opening the report use:

CODE
DoCmd.OpenReport RptName, acViewPreview


HTH.
DerekC
Thanks.
datAdrenaline
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
DerekC
Thanks for that approach as well, good for future reference.
datAdrenaline
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.

thumbup.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.