> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# The 'OpenReport' Action Was Canceled
- URL: https://nolongerset.com/the-openreport-action-was-canceled/
- Published: 2021-03-14T03:33:48.000Z
- Updated: 2026-05-08T13:05:55.000Z
- Description: Does it annoy you that using the Report NoData event as Codd intended results in a runtime error? Because it annoys me.
- Author: Mike Wolfe
- Tags: Reports, #Import 2026-05-20 02:59

There are several [things that annoy me](https://nolongerset.com/report-annoyances/) about the default behavior of **DoCmd.OpenReport**. To address these annoyances, I built a replacement function that I named **PreviewReport()**.

In this series of articles, I will take you through the evolution of this function as I address each of my frustrations.

## Part 3: Unwarranted Errors

Reports in Access have a [NoData event](https://docs.microsoft.com/en-us/office/vba/api/access.report.nodata) that triggers when there is, you guessed it, no data to display:

> The **NoData** event occurs after Microsoft Access formats a report for printing that has no data (the report is bound to an empty recordset), but before the report is printed. You can use this event to cancel printing of a blank report.

This allows you to show your users a friendly error message, like this:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/image-16.png)

Oh, okay, thanks for letting me know.

You can then cancel the opening of the report, to avoid the confusion that would result from seeing something like this:

![The Invoice report from the sample Northwind database](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/image-17.png)

What the dickens?! How come we keep invoicing that deadbeat **#Type!** when he never pays us?

### Sounds great, what's the problem?

Let's say you have the following code behind your report:

```vba
Private Sub Report_NoData(Cancel As Integer)
    MsgBox "There is nothing to report."
    Cancel = True
End Sub
```

When you run this report with criteria that filters out all the results, you'll get two messages. 

First, the message we want:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/2021-03-13-21_36_30-mjw20-Microsoft-Access-1.png)

Hello again, you look familiar.

Followed immediately by this rather unhelpful message:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/2021-03-13-21_45_05-mjw20-Microsoft-Visual-Basic-1.png)

Yes, yes, I know the OpenReport action was canceled. *I* am the one who canceled it. You just ruined my pleasant user experience, Access. I hope you're happy.

### What to do with error 2501?

I swallow that sucker whole. 

That's right, I ignore every instance of runtime error '2501.' And I mean every...single...instance. In fact, here's an excerpt from my [vbWatchdog global error handler](https://nolongerset.com/error-handling-evolution/):

```vba
Select Case ErrEx.Number
Case 2501  'Ignore OpenForm/Report action was canceled error messages
    If ErrEx.State <> OnErrorGotoLabel Then
        ErrEx.State = OnErrorResumeNext
    End If
'...other exceptions...
End Select
```

In all my years of development, globally swallowing this error has never bit me (knock on wood), but it has simplified my development immensely.

## Updating PreviewReport()

Even though I'm swallowing the error, I still like to know whether the report actually opened for the user or not. So, I updated my PreviewReport() function to return a Boolean that indicates whether the report was able to open or not:

```vba
'NOTE: This code assumes a vbWatchdog global error handler
'      with an exception for error 2501 (such as in the sample above)
Function PreviewReport(RptName As String, _
                       Optional Where As String = "") As Boolean
    If ReportIsOpen(RptName) Then DoCmd.Close acReport, RptName
    DoCmd.OpenReport RptName, acViewPreview, , Where
    If Err.Number = 2501 Then  'The OpenReport action was canceled
        PreviewReport = False
    Else
        PreviewReport = True
    End If
End Function

'source: https://nolongerset.com/why-so-lazy-access/
Function ReportIsOpen(RptName As String) As Boolean
    ReportIsOpen = (SysCmd(acSysCmdGetObjectState, acReport, RptName) <> 0)
End Function
```

The version I use (requires vbWatchdog)

As the comments above make clear, the previous code requires a vbWatchdog global error handler with the appropriate exception for error 2501.

If you are not using vbWatchdog, you can accomplish the same thing using a more traditional VBA approach to error handling:

```vba
Function PreviewReport(RptName As String, _
                       Optional Where As String = "") As Boolean
    On Error GoTo Err_PreviewReport

    If ReportIsOpen(RptName) Then DoCmd.Close acReport, RptName
    PreviewReport = True
    DoCmd.OpenReport RptName, acViewPreview, , Where

Exit_PreviewReport:
    Exit Function
Err_PreviewReport:
    Select Case Err.Number
    Case 2501   'The OpenReport action was canceled.
        PreviewReport = False
    Case Else
        MsgBox Err.Description, vbExclamation, "Error: " & Err.Number
    End Select
    Resume Exit_PreviewReport
End Function
```

The non-vbWatchdog version

### Recap

[Three down, two to go](https://nolongerset.com/report-annoyances/):

1. ~~**Auto-Print:** Opening a report sends it directly to the printer with no on-screen preview~~
2. ~~**Wrong Data:** If you call OpenReport on a report that's already open, Access doesn't apply the new criteria to the report~~
3. ~~**Unwarranted Errors:** When you cancel report opening in the OnNoData event, Access raises an error~~
4. **Cascading Maximize:** If you open a report from a windowed form, maximize the report, then close it, the windowed form from which you opened the report gets maximized
5. **Missing OpenArgs:** Prior to Access 2007, OpenArgs was not supported for the OpenReport method

*Image by [Gerd Altmann](https://pixabay.com/users/geralt-9301/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=4927525) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=4927525)*