Annoying message when 'Cancel = True' occurs

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys,
I have a couple of small bits of code within the 'on open' event of a form.
If a condition is true then the form immediately closes via the 'Cancel =
True' line. However, access then pops up with a message saying that the
"OpenForm action was canceled".... Is there a way of preventing this message
from appearing??

Here's one of the bits of code in case it helps:

If tCount("[TCAID]", "qry_CurrentDespatch", "TCAID = " &
[Forms]![frm_DespatchRun]![txtTCAID]) < 1 Then
MsgBox "You may not run any despatch reports for this request." _
& vbCrLf & vbCrLf & "Only requests that have reached Stages 1
through to 6 may be run.", vbExclamation, "TCA not yet ready"
Cancel = True
End If

Thanks in advance,

Lee
 
Public Sub TestSub()

On Error GoTo ErrorHandler
DoCmd.OpenForm "Form1"

ExitProcedure:
Exit Sub

ErrorHandler:
'2501 = error number for 'action was cancelled' error ...
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description, vbOKOnly
Resume ExitProcedure
End If

End Sub
 
sounds like you're opening the form from code that runs in another form,
specifically using the DoCmd.OpenForm action. that's where the error is
being generated. you can trap the error in the calling form's procedure and
handle it there, as

' place this line of code at the "top" of the procedure
On Error GoTo Handle_Err

' place these lines of code at the "bottom" of the procedure
Handle_Exit:
Exit Sub

Handle_Err:
Select Case Err.Number
Case 2501
' The OpenForm action was cancelled.
Resume Handle_Exit
Case Else
Msgbox Err.Number & ": " & Err.Description _
& vbCr & "Contact your system administrator.", _
"Unknown error"
Resume Handle_Exit
End Select

or you can avoid the error entirely by running the If statement validation
code in the calling form *instead of* in the second form's Open event, as

If tCount("[TCAID]", "qry_CurrentDespatch", "TCAID = " _
& [Forms]![frm_DespatchRun]![txtTCAID]) < 1 Then
MsgBox "You may not run any despatch reports for this request." _
& vbCrLf & vbCrLf & "Only requests that have reached " _
& "Stages 1 through to 6 may be run.", vbExclamation, _
"TCA not yet ready"
Else
DoCmd.OpenForm "FormName"
End If

hth
 
You have another form calling the form then you cancelled it. Ms Access
return the error number 2501.

Supposed you called the form from FORM_Calling then Add this error trapping,

Private Sub Command2_Click()
On Error GoTo Err_Trap
' call the form but you cancelled opening form1
DoCmd.OpenForm "form1", acNormal

Err_exit:
Exit Sub
Err_Trap:
Select Case Err.Number
Case 2501
Case Else
MsgBox Err.Description
End Select
Resume Err_exit
End Sub
 
Back
Top