Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2011
    Posts
    19

    Unanswered: "Runtime error 2501-The open report was cancelled"

    I have a query that when run uses the date field criteria to search between two sepcified dates:

    Between [Enter begining date (eg 01/06/11):] And [Enter Finish Date (eg 01/07/11):]

    When the report is run, the dialogue box first asks for the beginning date then end date, if the user enters a date then clicks ok it works fine. The problem is if the user selects cancel, it breaks and the above Runtime error is displayed.

    If the query is based on the following code:
    Code:
    Private Sub Command57_Click()
    If IsNull(Me.Combo51) Or Me.Combo51 = "" Then
                MsgBox "You must enter a directorate.", vbOKOnly, "Required Data"
                Me.Combo51.SetFocus
            Exit Sub
        End If
        DoCmd.Minimize
        DoCmd.OpenReport "rptDirectorate", acViewPreview
        DoCmd.Close acForm, "RunReports", acSaveNo
        
    End Sub
    is there a way of avoiding the error if cancel is clicked in the dialogue box?

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Try:
    Code:
    Private Sub Command57_Click()
    
        On Error Goto Err_Command57_Click
        If IsNull(Me.Combo51) Or Me.Combo51 = "" Then
                MsgBox "You must enter a directorate.", vbOKOnly, "Required Data"
                Me.Combo51.SetFocus
            Exit Sub
        End If
        DoCmd.Minimize
        DoCmd.OpenReport "rptDirectorate", acViewPreview
        DoCmd.Close acForm, "RunReports", acSaveNo
    
    Exit_Command57_Click:
        Exit Sub
    
    Err_Command57_Click:
        If Err.Number = 2501 Then
            Err.Clear
            Resume Next
        Else
            MsgBox "Error " & Err.Number & vbNewLine & Err.Description, vbExclamation, "An error occurred"
            Err.Clear
            Resume Exit_Command57_Click
        End If
    
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Aug 2011
    Posts
    19

    Runtime error 2501

    I have placed the code in my cmd button but it still breaks at
    Code:
    DoCmd.OpenReport "rptDirectorate", acViewPreview
    Regards

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    What are the error settings of your project? It should be: "Break on unhandled errors".
    Have a nice day!

  5. #5
    Join Date
    Aug 2011
    Posts
    19

    Runtime error 2501

    I have checked and it is set to Break on unhandled errors.
    Last edited by GinTop; 08-17-11 at 12:37. Reason: SQL not needed

  6. #6
    Join Date
    Aug 2011
    Posts
    19

    Runtime error 2501

    I have gone back and looked at the code I had copied over and realised my mistake.

    It now no longer breaks, but as the "RunReports" form is closed after the report is run, I am now left with neither open. The report is set OnClose to open the form "RunReports".

    Can I add DoCmd.OpenForm "RunReports" to this code so that if the error occurs, it will open up the form Run Reports and put the user back to the starting position?

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Simply change this:
    Code:
    Err_Command57_Click:
        If Err.Number = 2501 Then
            Err.Clear
            Exit_Command57_Click
    So the line:
    Code:
    DoCmd.Close acForm, "RunReports", acSaveNo
    will not be executed if the error 2501 occurs.
    Have a nice day!

Posting Permissions

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