Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access 2007 - Cancelling openreport from VBA code

Posted on 2009-05-04
2
Medium Priority
?
659 Views
Last Modified: 2013-11-28
I have a report which is opened from a menu. This report opens a dialog form from the report 'On Open' event to get a date range. On this form I have a 'Cancel' button in case the user changes their mind and this closes the form and sets a flag. This flag is checked by VBA in the report 'on open' event and exits from the report  with the statement 'Cancel=true'. However, this produces the error  2501 "The OpenReport Action was cancelled". I do not get this error if I open the report directly (ie not from my Menu form)
cancelrep = False
DoCmd.OpenForm "ChooseRepMonthYear", , , , , acDialog
 If cancelrep Then
   Cancel = True
   Exit Sub
End If

Open in new window

0
Comment
Question by:jeffkwells2003
2 Comments
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24293953
You need to add error handling to the routine that opens the report

Gary
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 24293960
You can simply "catch" the error:

On Error GoTo Err_Hand

cancelrep = False
DoCmd.OpenForm "ChooseRepMonthYear", , , , , acDialog
 If cancelrep Then
   Cancel = True
   Exit Sub
End If

Err_Hand:
  If Err.Number = 2501 Then
    Exit sub
  Else
    '/do something else here
  End If
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question