How To Overcome Error Message When No Data

Learn2010

Registered User.
Local time
Today, 07:39
Joined
Sep 15, 2010
Messages
415
I use the following code to close a form and open a selected report:

*** Start of Code ***

Application.Echo False, ""
DoCmd.SetWarnings False
DoCmd.Hourglass True
DoCmd.OpenQuery "qryLevyBilledHoldDel"
DoCmd.OpenQuery "qryUnpaidLevy"
DoCmd.OpenQuery "qryUnpaidWaiver"
DoCmd.OpenReport "rptUnpaidsByEmployee", acViewPreview
DoCmd.Maximize
DoCmd.Close acForm, "frmDailyLogOptions"
Application.Echo True
DoCmd.Hourglass False

***End of Code ***

On no data, I have found several ways to stop the process and give a message that there is no data. I have seen different ways of error handling. They are not working. Here is what I am trying to get to. If there are no records, give the no data message, stop the routine and return to my selection form frmDailyLogOptions.

Thank you.
 
...I have found several ways to stop the process and give a message that there is no data. I have seen different ways of error handling. They are not working....
I think you should describe what you've tried, and how it fails. It's a waste of time for both of us for me to describe an approach that you've already abandoned.
 
I keep getting the OpenReport was cancelled message.

I've tried this in the report:

***Start of Code***

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data found! Closing report."
Cancel = True
End Sub

***End of Code

And this:

***Start of Code***

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo Report_NoDataError

MsgBox "No records meet the report criteria"
Cancel = True

Report_NoDataExit:
Exit Sub

Report_NoDataError:
MsgBox Err.Description
Resume Report_NoDataExit
End Sub

***End of Code

And a few other variations. I put this in a few different places and could not get it to work.

***Start of Code***

Private Sub TestNoData_Click()
On Error Resume Next
DoCmd.OpenReport "SomeReport", acViewPreview
If Err = 2501 Then Err.Clear
End Sub

***End of Code

I just can't get it all together from start to finish. Thanks fo any help you might provide.
 
If you cancel the opening of the report in the report, this will cause an error in the calling code. The solution is to enable an error handler in the calling routine, and ignore the error 2501. So if we have a button on a form that opens a ReportA, and ReportA might cancel its own opening, then the button click routine on the form should look like this...
Code:
private sub cmdOpenReport_Click()
on error goto handler
   docmd.openreport "ReportA"
final:
   exit sub
handler:
   if err <> 2501 then msgbox err & " " & err.description
   resume final
end sub
See how that code selectively ignores error 2501?
Hope this helps,
 
That stopped it from opening. I will have to check it for a record with data. I will have to do that in the morning.

Thanks.
 

Users who are viewing this thread

Back
Top Bottom