Reputation: 2328
I'm generating a query and report through VBA. I have an option of asking the user if they want to output the report as a snapshot. I first ask them if they want to make a snap shot. If they say no, nothing happens. If they say yes, they get a prompt asking where they want to save it.
Everything works great except if they say yes and then click Cancel on the prompt, it raises a runtime error 2501 saying the report action was cancelled. Here is the code.
DoCmd.OpenReport "CONCERNS", acViewPreview, lstFee.Value & " DETAILS"
If MsgBox("Do you wish to create a snapshot of this report?", vbQuestion + vbYesNo) = vbYes Then
DoCmd.OutputTo acReport, "CONCERNS", "SnapshotFormat(*.snp)", ""
End If
This is also the end of my procedure so I don't really care if an error happens here since all the important stuff happened already. I just know some monkey somewhere will flip if they ever see it. Is there a way to handle this error? On Error Resume Next is not an option because that would make debugging a nightmare in the future. It sounds like I'm looking for something like a Try/Catch but I don't think VBA supports that.
Upvotes: 0
Views: 5701
Reputation: 1333
All you need is to handle the error ie:
On Error Goto HandleErr
DoCmd.OpenReport "CONCERNS", acViewPreview, lstFee.Value & " DETAILS"
If MsgBox("Do you wish to create a snapshot of this report?" _
, vbQuestion + vbYesNo) = vbYes Then
DoCmd.OutputTo acReport, "CONCERNS", "SnapshotFormat(*.snp)", ""
End If
ExitHere:
Exit Sub 'or Function
HandleError:
Select Case Err.Number
Case 2501 'Report Was Cancelled
If MsgBox ("Did you really want to cancel saving the report?", _
vbYesNo + vbDefaultButton2 ,"Please Confirm") = vbNo then
Resume
Else
Resume ExitHere
End if
Case Else
Msgbox "An Unexpected error Occurred " & Err.Description, _
vbExclamation,"Error"
Resume ExitHere
End Select
This will give the user the option to undo the cancel and let them know what they did.
Upvotes: 2
Reputation: 7019
On Error GoTo errHandler
....
Exit Sub
errHandler:
If (Err.Number = 2501) Then
Resume Next
End If
End Sub
Upvotes: 2
Reputation: 6169
There are (at least) two ways to handle this.
1> get the filename and handle possible cancellation in a step before sending the report snapshot. I haven't done this recently but there's another way to generate the snapshot report than the DoCmd.OutputTo command, or some variation that doesn't require the command itself to use a file dialog. I generated report snapshots in an old application and didn't have to ask the user for a filename. I'll try to find the code and show an example.
2> use On Error Resume Next, but only right before the DoCmd.OutputTo routine, then see if there's an error, then turn it back off:
If MsgBox("Do you wish to create a snapshot of this report?", _
vbQuestion + vbYesNo) = vbYes Then
On Error Resume Next
DoCmd.OutputTo acReport, "CONCERNS", "SnapshotFormat(*.snp)", ""
if Err.Number = 2501 Then
'' log or ignore error
Else
'' log or warn other unexpected errors
End If
On Error Goto 0
End If
Upvotes: 1