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

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
    Else
        'strReportDate = Day(CDate(Me.TxtDate)) & "/" & _
                'Month(CDate(Me.TxtDate)) & "/" & _
                'Year(CDate(Me.TxtDate))
         strReportDate = Month(CDate(Me.TxtDate)) & "/" & _
                Day(CDate(Me.TxtDate)) & "/" & _
                Year(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

Else
    RSPN.Close
   
   
   
    DoCmd.OpenForm "frmDayHistory", acPreview
   
End If
End Sub


eptdsadminAsked:
Who is Participating?
 
tbsgadiConnect With a Mentor Commented:
Hi eptdsadmin,

Check the references for Missing Reference
If nothing missing try decompiling on 2003 machine.
http://www.granite.ab.ca/access/decompile.htm

Good Luck!

Gary
0
 
NatchiketConnect With a Mentor Commented:
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.
0
 
mbizupConnect With a Mentor Commented:
> '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


0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Connect With a Mentor Database ArchitectCommented:
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?
0
 
Jeffrey CoachmanConnect With a Mentor MIS LiasonCommented:
eptdsadmin,


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_YourSubName:
    Exit Sub

Err_YourSubName:
    '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
    Else
        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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.