No Data on Report .  .  .  Still get error about Report Was Canceled . . . Need to Suppress

gdunn59
gdunn59 used Ask the Experts™
on
I have a button that runs a report, and I have code in the NoData Event of the Report (see below), but I still get the message that the Report was canceled.  How do I suppress this?

Code on the NoData Event of the Report:
MsgBox "No Data Found For This Employee", vbExclamation, "No Data"
  Cancel = True
End Sub

Open in new window



Code for Button:
Private Sub cmdAssocRpt_Click()
On Error GoTo Err_cmdAssocRpt_Click
Dim stDocName As String

DoCmd.SetWarnings False

If IsNull(Me.cboReportCateg) Or IsNull(Me.cboCategSelect) Then
    MsgBox "Please make selections from the drop-downs for a Auditor, Department, Employee or Manager", vbOKOnly
    Me.cboReportCateg.SetFocus
    Me.cboReportCateg.Dropdown
ElseIf Me.cboReportCateg = "Employee" Then
    stDocName = "rptAssociate_Report"
     DoCmd.OpenReport stDocName, acPreview
End If

Exit_cmdAssocRpt_Click:
    Exit Sub

Err_cmdAssocRpt_Click:
If Err.Number = 2501 Then
'no action required - ignore the error - because opening of report was cancelled
Else
    MsgBox Err.Description
End If
     
DoCmd.GoToControl "cboCategSelect"
Me.cboCategSelect = Null
Me.cboReportCateg = Null
Me.txtBeginDT = Null
Me.txtEndDT = Null
Me.cboDept = Null

GoTo Exit_cmdAssocRpt_Click

DoCmd.SetWarnings True
 
End Sub

Open in new window


Thanks,
gdunn59
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
First ... get rid of the DoCmd.SetWarnings  False - very dangerous. And the way you have it, if no error occurs, it never gets set back to True.  You should not need this here.

I see you are already trapping for error 2501, which normally would work.  Is that the error you are getting?  Occasionally, I've seen a slightly different error in this scenario.

mx

Author

Commented:
Yes 2501 is the error I'm getting.
Most Valuable Expert 2012
Top Expert 2013
Commented:
In the vba editor

Tools. --> options

Make sure your error option is set to

Break on unhandled errors
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Author

Commented:
Here is the exact error message I am getting:

  Run-time error '2501':

  The OpenReport action was canceled.

Author

Commented:
It had "Break on All Errors" selected.  is that okay or do I need to select the option you mentioned above "Break on unhandled errors"?
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
"It had "Break on All Errors" selected"
That is your problem ... set to Unhanded Errors

Author

Commented:
Ok.  That worked.

Thanks,
gdunn59
Most Valuable Expert 2012
Top Expert 2013

Commented:
That would do it.  Break on all errors should only be used to debug your code
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
I think mbizup noted this first, or at least implied it. Actually, I didn't even see that post.

Be *SURE* to get rid of DoCmd.SetWarnings ...

mx
Most Valuable Expert 2012
Top Expert 2013

Commented:
Thanks, mx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial