ChrisO
Registered User.
- Local time
- Today, 19:36
- Joined
- Apr 30, 2003
- Messages
- 3,202
Cancel = True
There appears to be a misunderstanding on the www about what
Cancel = True
means specifically in the Open event of a Form or Report.
One example:-
http://allenbrowne.com/casu-20.html
That example implies that, if we wish to close the Form, we should use Cancel = True. That example then goes on to say that the Cancel will raise a VBA error in the Form which attempted to open the Form which was cancelled.
Specifically, the error raised is error “2501; The OpenForm action was canceled.”
That then leads people to write code like this… (Simplified)
In the Form which is cancelled:-
And in the Form which opened it:-
Now while the above code works as expected it does not lead to an understanding.
In the Form which closes itself
Cancel = True
does two things. It both closes the Form and it raises the error in the Form which attempted to open it. But the Form which attempted to open it ignores the error in the Select Case structure in its error handler.
So the question then becomes; why raise an error if that error will be ignored?
The alternative to the above code then becomes…
In the Form which is cancelled:-
And in the Form which opened it:-
I understand that people will jump to the defence of what and who they read on the www and what they have done themselves in the past.
But did they know that
Cancel = True
is specifically designed to raise an error which gets passed back but also gets ignored?
Can anybody post code which actually uses the error which
Cancel = True
produces?
Not just words please but actual copy/paste code from a past application.
What I’m trying to find out is if
Cancel = True
is any better, because it raises an error, than
DoCmd.Close acForm, Me.Name
which does not raise the error.
Chris.
There appears to be a misunderstanding on the www about what
Cancel = True
means specifically in the Open event of a Form or Report.
One example:-
http://allenbrowne.com/casu-20.html
That example implies that, if we wish to close the Form, we should use Cancel = True. That example then goes on to say that the Cancel will raise a VBA error in the Form which attempted to open the Form which was cancelled.
Specifically, the error raised is error “2501; The OpenForm action was canceled.”
That then leads people to write code like this… (Simplified)
In the Form which is cancelled:-
Code:
Private Sub Form_Open(Cancel As Integer)
Cancel = True
End Sub
And in the Form which opened it:-
Code:
Private Sub cmdOpenForm_Click()
On Error GoTo ErrorHandler
DoCmd.OpenForm "frmMyForm"
ExitProcedure:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501 [color=green]' Cancelled
' Do nothing.[/color]
Case Else
MsgBox "Error " & Err.Number & " " & Err.Description
End Select
Resume ExitProcedure
End Sub
Now while the above code works as expected it does not lead to an understanding.
In the Form which closes itself
Cancel = True
does two things. It both closes the Form and it raises the error in the Form which attempted to open it. But the Form which attempted to open it ignores the error in the Select Case structure in its error handler.
So the question then becomes; why raise an error if that error will be ignored?
The alternative to the above code then becomes…
In the Form which is cancelled:-
Code:
Private Sub Form_Open(Cancel As Integer)
DoCmd.Close acForm, Me.Name
End Sub
And in the Form which opened it:-
Code:
Private Sub cmdOpenForm_Click()
On Error GoTo ErrorHandler
DoCmd.OpenForm "frmMyForm"
ExitProcedure:
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & " " & Err.Description
Resume ExitProcedure
End Sub
I understand that people will jump to the defence of what and who they read on the www and what they have done themselves in the past.
But did they know that
Cancel = True
is specifically designed to raise an error which gets passed back but also gets ignored?
Can anybody post code which actually uses the error which
Cancel = True
produces?
Not just words please but actual copy/paste code from a past application.
What I’m trying to find out is if
Cancel = True
is any better, because it raises an error, than
DoCmd.Close acForm, Me.Name
which does not raise the error.
Chris.