Microsoft Access
--
Questions
--
Followers
Top Experts
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?
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Private Sub Form_Open(Cancel As Integer)
  If Me.RecordsetClone.recordCo
     MsgBox "No Invoices/Bookings are associated to this Name. Please try another search."
     Cancel = True
  End If
End Sub
mx
What is the code that opens this form?
You will need to trap for Cancelling the open.
mx






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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
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/M
mx
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_Clic
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

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
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.recordCo
     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?
Not sure what you are asking ?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
  If Me.RecordsetClone.recordCo
     MsgBox "No Invoices/Bookings are associated to this Name."
     Cancel = True
  End If
End Sub
Not much chance of an unexpected error really.
And remove the On Error Resume Next

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Microsoft Access
--
Questions
--
Followers
Top Experts
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.