Run time error 2501

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tomric
    New Member
    • Nov 2009
    • 31

    Run time error 2501

    Hi, I have an option group to select different reports, each report is based off a query that asks for criteria for that report. I have no problem opening any the querys through the group regardless of the data involved. The problem arises when you go to open a report and click the cancel button on the criteria question. I get a "Run Time Error 2501", "The Open Report Action was canceled". The error occurs only on the reoprt in the elseif statement. The grpreport = 6 opens other forms that have the criteria on the form, because there are 14 different criterias the query looks at. The grprrport = 6 also uses a macro for that specific report. The code I used is as to open the reports is as follows:

    Code:
    Private Sub cmdreport_Click()
    On Error GoTo Err_cmbreport_Click
    
     Dim stDocName As String
     Dim stLinkCriteria As String
        
    
    
    If Me.grpreports = 6 Then
    
        DoCmd.Close
    
        stDocName = "design type"
        DoCmd.OpenForm stDocName, , , stLinkCriteria
     
    ElseIf Me.grpreports = 1 Then
    
        DoCmd.Close
    
        DoCmd.OpenReport "raw data for a bar", acViewReport
        
        
    Exit_cmbreport_Click:
     Exit Sub
     
        If Err.Number = 2501 Then
            Resume Next
        Else
    Err_cmbreport_Click:
            MsgBox Err.Description
            Resume Exit_cmbreport_Click
       End If
    
    End If
    
    
    End Sub
    The program stops at the DoCmd.openrepor t line. I won't added the rest of the option buttons for reports untill I get past this problem. Can anyone help me capture the error?
    Last edited by Jim Doherty; Jun 3 '10, 07:15 PM. Reason: Code tags
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    I think some of the lines in your code were out of order. Try this:

    Code:
    Private Sub cmdreport_Click()
    
    On Error GoTo Err_cmdreport_Click
    
    Dim stDocName As String
    Dim stLinkCriteria As String
    
    If Me.grpreports = 6 Then
    
       DoCmd.Close
       stDocName = "design type"
       DoCmd.OpenForm stDocName, , , stLinkCriteria
    
    ElseIf Me.grpreports = 1 Then
    
       DoCmd.Close
       DoCmd.OpenReport "raw data for a bar", acViewReport
    
    End If
    
    Exit_cmdreport_Click:
    
       Exit Sub
    
    Err_cmdreport_Click:
    
       If Err.Number = 2501 Then
          Resume Next
       Else
          MsgBox Err.Description
          Resume Exit_cmdreport_Click
       End If
    
    End Sub

    You had the test for error 2501 before the GoTo label for subroutine error handling. This means that when the error occurs, it will skip past your test and display the error anyway.

    Also, I think you put your last End If too far to the bottom of the subroutine, so I moved it up before the GoTo labels for Exit and Error handling (which are usually the last things to appear in a subroutine).

    Finally, you had your GoTo labels "cmb" instead of "cmd"...

    Pat
    Last edited by patjones; Jun 3 '10, 07:03 PM. Reason: Caught one more possible problem...

    Comment

    • tomric
      New Member
      • Nov 2009
      • 31

      #3
      Thank you, but I'm still getting the "Run Time Error 2501". Have any other sugestions? I put a break in at the error sub routine and steped through it, it doesn't go into the routine before I get the error.

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        I would suggest putting a break somewhere near the top of the subroutine and stepping through it from that point to see exactly where the error is raised.

        Pat

        Comment

        • tomric
          New Member
          • Nov 2009
          • 31

          #5
          I tried that, it doesn't go into the subroutine. If I go through and run the report it works with no problems. It's just when I hit cancel it stops at the DoCmd.OpenRepor t line. I also tried steping through from the begining of the procedure, it gets to that line, opens the report which opens a query, hit cancel from the criteria question, goes back to that line and stops with the error.

          Comment

          • patjones
            Recognized Expert Contributor
            • Jun 2007
            • 931

            #6
            But if you enter criteria and continue then the report opens correctly?

            I think the problem is that when you hit "Cancel", the query is not getting parameters that it needs to in order to construct a record source for the report. As a result the report fails to open.

            Pat

            Comment

            • tomric
              New Member
              • Nov 2009
              • 31

              #7
              Yes that is correct, the report will run just fine. it's the cancel button that's the problem. If I try running the report by itself, with out the code from the form, and click cancel it works ok.

              Comment

              • patjones
                Recognized Expert Contributor
                • Jun 2007
                • 931

                #8
                I think the way for you to solve this is to determine which set of criteria you need some other way - say by entering it on the form, then passing that criteria to the report using the filter argument of OpenReport. This is generally how a report is filtered.

                As a simple example of how this works, suppose I have a table called tblEmployees, with fields fldEmplID, fldLastName, fldFirstName, fldDateOfBirth.

                In the Open event of a report based on this table, I can write, very simply:

                Code:
                Me.RecordSource = "SELECT * FROM tblEmployees"

                This will pick everything from the table. But what if you want to restrict the results to everyone with the last name "Smith"? When you call the report from the form, you can use the Filter argument of the OpenReport call:

                Code:
                DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldLastName = 'Smith'"

                Or what about everyone with a date of birth in 1985?

                Code:
                DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldDateOfBirth BETWEEN #12/31/1984# AND #1/1/1986#"

                As you can see, the filter criteria is passed in as a string, and it's not hard to see how you can call the report with different criteria depending on some other condition...lik e in your case - if option button 3 is selected you'll use one filter, but if option button 4 is selected you'll use a different filter.

                It might take a little more coding on your part to make this work, but in the long run it will pay for you to organize it like this, I think.

                Pat

                Comment

                • tomric
                  New Member
                  • Nov 2009
                  • 31

                  #9
                  Thank you for your help.

                  Comment

                  Working...