?
Solved

Access 2010 - The OpenForm action was cancelled

Posted on 2012-09-13
13
Medium Priority
?
3,686 Views
Last Modified: 2012-09-13
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
Comment
Question by:andrewpiconnect
  • 6
  • 5
  • 2
13 Comments
 
LVL 75
ID: 38396259
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
 

Author Comment

by:andrewpiconnect
ID: 38396301
Thanks mx but I still get the same error msg
0
 
LVL 75
ID: 38396317
I see.
What is the code that opens this form?

You will need to trap for Cancelling the open.

mx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 61

Expert Comment

by:mbizup
ID: 38396322
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
 
LVL 75
ID: 38396323
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
 

Author Comment

by:andrewpiconnect
ID: 38396391
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
 
LVL 61

Expert Comment

by:mbizup
ID: 38396409
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
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 38396412
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
 

Author Closing Comment

by:andrewpiconnect
ID: 38396574
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
 
LVL 75
ID: 38396621
"second code?"

Not sure what you are asking ?
0
 

Author Comment

by:andrewpiconnect
ID: 38396664
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
 
LVL 75
ID: 38396683
No error trapping is necessary there per se.  
Not much chance of an unexpected error really.

And remove the On Error Resume Next
0
 

Author Comment

by:andrewpiconnect
ID: 38396712
excellent...thanks for your help...much appreciated
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

862 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