Run-time error '2501': The OpenForm action was cancelled

Posted on 2007-10-17
Last Modified: 2010-05-18
Users get the run time error in 2003 when trying to open a form called frmDayHistory. Funny thing is it doesnt happen in 2007, and was thinking it may be reference llibrary?? just guessing...

Anyway here is the code which fails on the very last line "Docmd.Openform"...

Private Sub Command6_Click()

Dim BCSDb As DAO.Database
Dim RSPN As DAO.Recordset
Dim sqlinfo As String

Dim strReportDate As String
Dim ReportDate As String

    If Len(Me.TxtDate & vbNullString) = 0 Then
        MsgBox "Please ensure that a report date is entered into the form", _
               vbInformation, "Required Data..."
        Exit Sub
        'strReportDate = Day(CDate(Me.TxtDate)) & "/" & _
                'Month(CDate(Me.TxtDate)) & "/" & _
         strReportDate = Month(CDate(Me.TxtDate)) & "/" & _
                Day(CDate(Me.TxtDate)) & "/" & _
   ReportDate = "(#" & strReportDate & "#)"
   'MsgBox ReportDate, vbInformation
   DoCmd.SetWarnings False
   DoCmd.RunSQL "UPDATE tblDate SET ReportDate = " & ReportDate & ""
    End If

sqlinfo = "SELECT * FROM tblScrapRecords WHERE Date = " & ReportDate & ""
'MsgBox sqlinfo, vbInformation

Set BCSDb = CurrentDb
Set RSPN = BCSDb.OpenRecordset(sqlinfo, dbOpenSnapshot)

If RSPN.BOF = True And RSPN.EOF = True Then
MsgBox "There are no records for " & Me.TxtDate & ". Please ensure you have entered the correct date "
  Exit Sub

    DoCmd.OpenForm "frmDayHistory", acPreview
End If
End Sub

Question by:eptdsadmin
    LVL 46

    Accepted Solution

    Hi eptdsadmin,

    Check the references for Missing Reference
    If nothing missing try decompiling on 2003 machine.

    Good Luck!

    LVL 17

    Assisted Solution

    Barring any code in the on_open event of the form, I'm wondering what the purpose of using acPreview as the View parameter for the OpenForm method is.  I would have thought acNormal (or even better, no argument) would be more approriate.

    Unless you are treating your form as some sort of report, in which case the print preview view would be suitable, in which case I should think that you need ensure that a printer is set up.
    LVL 61

    Assisted Solution

    > '2501': The OpenForm action was cancelled
    This means that the open event of your form failed.  Do you have any code in the open or load event of this form (such as setting recordsource or filters)?

    I nnoticed that you are not using date delimiters in your SQL.  In Access, if these are Date fields (not text or numeric), you need to delimit dates with # signs:

     DoCmd.RunSQL "UPDATE tblDate SET ReportDate = #" & ReportDate & "#"

    sqlinfo = "SELECT * FROM tblScrapRecords WHERE [Date] = #" & ReportDate & "#"
                                                                                  ^^^---- Date is a reserved word, and as a field name should be enclosed in brackets

    LVL 75

    Assisted Solution

    by:DatabaseMX (Joe Anderson - Access MVP)
    Here is a typical example where that error occurs, which may help you track it down:

    You have button which opens a report ..
       DoCmd.OpenReport "YourReport"

    But ... the report has No Data ... and in the OnOpen event of the report, you have a message box that pops up and

    says "Sorry, no Data"  if the NoData property = True ... and then you Cancel the opening of the report. Back at the

    Open command ... you will the get the message ""You cancelled the previous operaton".

    So ... you have to trap that error around the OpenReport command.

    So ... maybe this example will help you track it down?
    LVL 74

    Assisted Solution

    by:Jeffrey Coachman

    Here is the error handler I use.
    It is one way to implement DatabaseMX's reasoning

    On Error GoTo Err_YourSubName

    ********** Your Code Goes Here **********

        Exit Sub

        'If the "Open" event of the Report or Form itself fails
        'Because this is the "calling" sub
        '(ex. NoData, Or RecordCount<=1)
        If Err.Number = 2501 Then
            Resume Exit_YourSubName
            MsgBox "There was an error executing the command." _
            & vbCrLf & vbCrLf & "Error " & Err.Number & ": " _
            & vbCrLf & vbCrLf & Error, vbExclamation
            Resume Exit_YourSubName
        End If

    HTH as well

    Jeff Coachman

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now