• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4069
  • Last Modified:

Access 2010 - The OpenForm action was cancelled

Hi,

I have a form which runs the following code on the OnLoad event. However, I am getting an error message "The OpenForm action was cancelled" after the form closes.

I have tried using DoCmd.SetWarnings = False / True but this still does not stop the error message.

Dim rs As dao.Recordset
    Set rs = Me.RecordsetClone
    If rs.recordCount = 0 Then
         MsgBox "No Invoices/Bookings are associated to this Name. Please try another search."
         DoCmd.Close acForm, "frmAmendInvoiceList"
    End If

Any ideas please?
0
andrewpiconnect
Asked:
andrewpiconnect
  • 6
  • 5
  • 2
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Try this in the On Open event instead


Private Sub Form_Open(Cancel As Integer)
    If Me.RecordsetClone.recordCount = 0 Then
         MsgBox "No Invoices/Bookings are associated to this Name. Please try another search."
         Cancel = True
    End If
End Sub

mx
0
 
andrewpiconnectAuthor Commented:
Thanks mx but I still get the same error msg
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
I see.
What is the code that opens this form?

You will need to trap for Cancelling the open.

mx
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mbizupCommented:
The error you are mentioning occurs in whatever procedure opens the form. The error occurs whether the form fails to open or whether you are intentionally cancelling the open event.  So in addition to checking for no records through the forms open event, you also need to handle this error in the sub or function where you are opening the form:

For example, if it is a command button click event:

Private Sub cmdYourButton_Click()
         On Error goto EH
          Docmd.OpenForm "YourFormName"
         exit sub
eh:
      if Err.Number = 2501 then resume next    '<--- double check the error number here
end sub

Open in new window

0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Here is the idea (except Report was used in this example)

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 in this example ... you have to trap that error around the OpenReport command.

So ... maybe this example will help you track it down?
************************************
Private Sub cmdPrintApp_Click()
    On Error GoTo Err_cmdPrintApp_Click
    DoCmd.OpenReport "SomeReportName", acPreview

Exit_cmdPrintApp_Click:
    Exit Sub
Err_cmdPrintApp_Click:
    If Err.Number = 2501 then   ' You cancelled .....
         ' no action required
    Else
         MsgBox Err.Description
    End If
    Err.Clear
    Goto Exit_cmdPrintApp_Click
End Sub

om/Microsoft/Development/MS_Access/Access_Coding-Macros/Q_23753771.html

mx
0
 
andrewpiconnectAuthor Commented:
Hmmmm.

Here is my code which opens the form. I might add that there may well be a Last Name in the tblHAPassengers but if there is no bookingID associated to that name in the tblHABookings the form will open but no records show.

I am trying to either (1) amend the opening code or (2) close the form as previously described but without the error msg.


Private Sub cmdAmendInvoiceNumber_Click()
On Error GoTo cmdAmendInvoiceNumber_Err

    Dim strInput As String
    Dim recordCount1 As Integer
    Dim recordCount2 As Integer
    Dim tryAgain As Integer
   
    'enter loop
    Do
        strInput = InputBox("Please enter a ""Booking Ref"" or ""Passenger Last Name"".")

        If Len(strInput) <> 0 Then
            'look for Booking Ref or Passenger Last Name
            recordCount1 = Nz(DCount("*", "tblHABookings", "[BookingRef] = '" & strInput & "'"), 0)
            recordCount2 = Nz(DCount("*", "tblHAPassengers", "[LastName] = '" & strInput & "'"), 0)

            If recordCount1 <> 0 Or recordCount2 <> 0 Then
                tryAgain = vbNo
                If recordCount1 <> 0 Then
                DoCmd.OpenForm "frmAmendInvoiceList", , , "BookingRef='" & strInput & "'"
                End If
                If recordCount2 <> 0 Then
   ' PERHAPS I NEED ANOTHER SEARCH HERE TO DETERMINE WHETHER THE tblHABookings
   ' has a bookingID associated with the Passenger Name
                DoCmd.OpenForm "frmAmendInvoiceList", , , "LastName='" & strInput & "'"
                End If
            Else
                tryAgain = MsgBox("Booking Ref or Passenger Last Name '" & strInput & "' was not found. Try again?", vbYesNo)
            End If
           
        Else
            tryAgain = vbNo
        End If
    Loop Until tryAgain = vbNo

cmdAmendInvoiceNumber_Exit:
    Exit Sub

cmdAmendInvoiceNumber_Err:
    MsgBox Error$
    Resume cmdAmendInvoiceNumber_Exit
End Sub
0
 
mbizupCommented:
You should do both.   Cancel the open event when appropriate as mx suggested and use error handling when opening the form to handle that error that gets generated when the Open  event is canceled
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
On error Resume Next
DoCmd.OpenForm "frmAmendInvoiceList", , , "LastName='" & strInput & "'"
If Err.Number = 0 OR Err.Number = 2501
   ' no action
   Err.Clear ' or reset On Error to other location
Else
   msgbox "Unexpected Error .."
   ' do whatever
End If
0
 
andrewpiconnectAuthor Commented:
Excellent MX! Its solved!!

I added thsi code in the first form:

If recordCount2 <> 0 Then
'experts code
On Error GoTo Err_OpenList_Click
    DoCmd.OpenForm "frmAmendInvoiceList", , , "LastName='" & strInput & "'"
                   
Exit_OpenList_Click:
    Exit Sub
Err_OpenList_Click:
    If Err.Number = 2501 Then   ' You cancelled .....
    ' no action required
    Else
    MsgBox Err.Description
    End If
Err.Clear
GoTo Exit_OpenList_Click
'experts code
                End If

And this code to the opened form:

Private Sub Form_Open(Cancel As Integer)
On Error Resume Next

    If Me.RecordsetClone.recordCount = 0 Then
         MsgBox "No Invoices/Bookings are associated to this Name."
         Cancel = True
    End If

End Sub

Should i use some error trapping in the second code?
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"second code?"

Not sure what you are asking ?
0
 
andrewpiconnectAuthor Commented:
is this code .... on the On Open event in the new form ok or should i use some error trapping in this as well?

Private Sub Form_Open(Cancel As Integer)
On Error Resume Next

    If Me.RecordsetClone.recordCount = 0 Then
         MsgBox "No Invoices/Bookings are associated to this Name."
         Cancel = True
    End If

End Sub
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
No error trapping is necessary there per se.  
Not much chance of an unexpected error really.

And remove the On Error Resume Next
0
 
andrewpiconnectAuthor Commented:
excellent...thanks for your help...much appreciated
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now