How do you prevent access from showing error dialog box (Runtime error 2501).

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Brown
    New Member
    • Jul 2010
    • 16

    How do you prevent access from showing error dialog box (Runtime error 2501).

    I keep on getting the same error message and I don't know how to either solve the problem or prevent it from showing the error. I have set up a switchboard item that opens a report. When the report opens it will call a form so that the user can change parameters in the report and filter the information before the report is generated see below.


    Code:
    Private Sub Report_Open(Cancel As Integer)
    DoCmd.OpenForm "CostCodeFilter", , , , acFormAdd, 3
    
    'Triggered object in the form will let the program know if the user has cancelled the report.
    If Reports![Cost Code Stats].Controls("Label14").Visible = False Then   
        Cancel = True
    End If
    End Sub
    If the user closes the filter box it will cancel the report generation but for some reason it always spits out the same error message.

    I have tried using error handling in the code but the problem is that the error is generated outside of the open event and the error handling is not triggered.

    Thanks for help.
  • Marc Brown
    New Member
    • Jul 2010
    • 16

    #2
    Just to clarify..

    The switch board runs prompts the function CCS_Run

    Code:
    Function CCS_Run()
    DoCmd.OpenReport "Cost Code Stats", acViewPreview
    End Function
    That function loads the the report which has an open event

    Code:
    Private Sub Report_Open(Cancel As Integer)
    
    DoCmd.OpenForm "CostCodeFilter", , , , acFormAdd, 3
    'Cost code filter will load a form that allows the user to filter the report.
    
    If Reports![Cost Code Stats].Controls("Label14").Visible = False Then: Cancel = True
    
    End Sub
    I found that if I open the report from database instead of through the switch board I do not get the error message otherwise the error will come up.

    Does it create problems if I have nested DoCmd.Open commands?

    Comment

    • Marc Brown
      New Member
      • Jul 2010
      • 16

      #3
      The answer is yes it does create problems. There has to be an error handle in the function CCS_Run to catch the event that the report has been cancelled.

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        The error is being raised by line 2:
        Code:
        Function CCS_Run()
          DoCmd.OpenReport "Cost Code Stats", acViewPreview
        End Function
        when the report is cancelled.

        Check for the error, and ignore it.

        Code:
        Function CCS_Run()
          On Error Goto ErrHandler
          DoCmd.OpenReport "Cost Code Stats", acViewPreview
        
        ExitFunction:
          Exit Function
        
        ErrHandler:
          If Err.Number=2501 then
            'User cancelled report, ignore and exit
            Resume ExitFunction
          Else
            Msgbox Err.Number & " - " & err.Description
            Resume ExitFunction
          End If
        End Function

        Comment

        Working...