I have encountered a problem in Access 2010 for Windows:
I discovered this while using doCmd.OpenReport to launch a report. The report opens a form in the Report_Open event. The form prompts the user for report parameters and allows them to press "OK" to display the report or press "Cancel". The On Click event for "Cancel" sets "Cancel = True".
It appears that OpenReport() is not responding gracefully to the cancel. This seems like a frequently used technique so I hesitate to call it a bug and am wondering if I am doing something wrong. Actually... the more I explain this, it does sound like a bug. At this point, I am hoping somebody has a workaround or that I am missing something obvious.
This is just a simplified example I created to illustrate the problem:
form
Private Sub Form_Open(Cancel As Integer)
On Error GoTo errHandler
DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog
Exit Sub
errHandler:
Select Case Err.Number
Case 2501 ' Cancelled by user, or by NoData event.
MsgBox "Report cancelled, or no matching data.", vbInformation, "Information"
Case Else
MsgBox "Error " & Err & ": " & Error$, vbInformation, "Form_Open()"
End Select
Resume Next
End Sub
report
Private Sub Report_Open(Cancel As Integer)
Cancel = True
End Sub
The reason for the problem in this case is acDialog:
DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog
I think you will find:
DoCmd.OpenReport "Test Report", acViewPreview
Works without problems.
Edit re Comment
You should not need to cancel the report. For the most part, it is better to avoid errors than to trap them, so check for data and get parameters before opening the form. Move the form from the report open event to step of its own and use DLookUp or a query to check for data before launching the form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With