Cancel Report

D

DDBeards

On Open of a report I run code to open a form for the user to select filters
for the report and it works great. However, my users want a cancel button on
this form to cancel the report. I added the cancel button to the form and
can close the form with no problem, but I can not figure out how to cancel
the report itself. What makes this a little tricky is that this form is
called by many reports so I would need a way to capture the name of the
report to cancel! Please Help

Thanks DDBeards
 
K

Klatuu

You can use the OpenArgs argument when you open the form from the report to
pass the reports name to the form. Then in the form, create a module level
varialbe to store the report name. Populate the variable in the form's Load
event:

If Not IsNull(Me.OpenArgs) Then
strReportName = Me.OpenArgs
End If

Then when you close the form, you can close the report:

Docmd.Close acReport, strReportName, acSaveNo

Now, it may be that when you close the form, you get an error 2501. I am
not sure, because I haven't tested it, but it is not unusual to get a 2501 if
the report is canceled. If so, you will have to deal with that in whatever
procedure opens the form.
 
F

fredg

On Open of a report I run code to open a form for the user to select filters
for the report and it works great. However, my users want a cancel button on
this form to cancel the report. I added the cancel button to the form and
can close the form with no problem, but I can not figure out how to cancel
the report itself. What makes this a little tricky is that this form is
called by many reports so I would need a way to capture the name of the
report to cancel! Please Help

Thanks DDBeards

What version of Access?
Access 97 or older? Post back.
Access 2000 or newer?

Code the report's Open event:

' first open the form here however you are already doing it.
DoCmd.OpenForm "FormName", , , , , acDialog

' Then check to see if the form is still open
If Not CurrentProject.AllForms("FormName").IsLoaded Then
' The Form is not open, cancel the report
MsgBox "The report will not be run."
Cancel = True
End If

Note: If the report has been opened using VBA from an event, this will
generate Error 2501.
You must trap the error in the code that you used to open the report:

On Error GoTo Err_Handler
DoCmd.OpenReport "ReportName", acViewPreview

Exit_this_Sub:
Exit Sub
Err_Handler:
If Err = 2501 Then
Else
MsgBox "Error #: " & Err.Number & " " & Err.Description
End If
Resume Exit_This_Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top