PDA

View Full Version : Handling a specific error on running reports



knifelong
12-12-2012, 04:37 AM
Hi
I'm running multiple reports with VBA DoCmd.OutputTo method.

However I sometimes get two specific errors, A 2501 no records error which causes the procedure to exit and a 3146 odbc call failed which also causes the procedure to exit.

I would like to handle it so that when I get the 2501 error then it ignores the error and moves to the next report and when I get the 3146 error it runs the same report again as it failed.

However I'm not sure the best way to ignore the error and to get the same report to run again.


thanks




On Error Resume next


DoCmd.OutputTo acOutputReport, "my report", acFormatRTF, "C:\report.rtf", False
if Err.Number = 2501 Then
' ignore error

Else
if Err.Number = 3146 Then
Resume
End If
On Error Goto 0 'turn error reporting back on

BrianMH
12-12-2012, 11:13 AM
On Error goto errhandler
retry:
DoCmd.OutputTo acOutputReport, "my report", acFormatRTF, "C:\report.rtf", False

exit sub
errhandler:
If Err.Number = 2501 Then resume next 'Or possibly exit sub
Else If Err.Number = 3146 Then
goto retry
End If
On Error Goto 0 'turn error reporting back on

knifelong
12-12-2012, 01:46 PM
Thanks for your help !

So for multiple reports I did something like this for now, but I guess there is a better way using a loop and without having to keep repeating the error handler.







'report1
On Error GoTo errhandler1
retry1:
MsgBox ("'report1")
DoCmd.OutputTo acOutputReport, "'report1", acFormatRTF, "C:\'report1.rtf", False

errhandler1:
If Err.Number = 2501 Then
Resume Next 'Or possibly exit sub
ElseIf Err.Number = 3146 Then
GoTo retry1
End If
On Error GoTo 0 'turn error reporting back on





'report2
On Error GoTo errhandler2
retry2:
MsgBox ("'report2")
DoCmd.OutputTo acOutputReport, "'report2", acFormatRTF, "C:\'report2.rtf", False

errhandler2:
If Err.Number = 2501 Then
Resume Next 'Or possibly exit sub
ElseIf Err.Number = 3146 Then
GoTo retry2
End If
On Error GoTo 0 'turn error reporting back on

BrianMH
12-13-2012, 07:37 AM
The better way of doing this would be to create a sub where you pass the variables to it.


Sub outputreport(sReport As String, sPath As String)
On Error GoTo errhandler
retry:
DoCmd.OutputTo acOutputReport, sReport, acFormatRTF, sPath, False

Exit Sub
errhandler:
If Err.Number = 2501 Then
Resume Next 'Or possibly exit sub depending on what you want to do
ElseIf Err.Number = 3146 Then
GoTo retry
Else: MsgBox Err.Number & " - " & Err.Description
End If
End Sub
Sub callreport()
Call outputreport("'report1", "C:\'report1.rtf")
Call outputreport("'report2", "C:\'report2.rtf")
'repeat above as necessary with appropriate variables
End Sub