Consulting

Results 1 to 4 of 4

Thread: Handling a specific error on running reports

  1. #1

    Handling a specific error on running reports

    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



    [VBA]
    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

    [/VBA]

  2. #2
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    [VBA]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[/VBA]
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  3. #3
    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.




    [VBA]


    '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


    [/VBA]

  4. #4
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    The better way of doing this would be to create a sub where you pass the variables to it.

    [VBA]
    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
    [/VBA]
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •