3.1 An example
1Private Sub Form_BeforeUpdate(
Cancel As
Integer)
2 If ([Country/Region].Value = ”USA”) And _
3 Not ([Zip/Postal Code].Value like ”#####”)
Then 4 Cancel = True
5 End If
6End Sub
This is a handler of the “Before Update” event. The name of the subroutine is Form_BeforeUpdate. The conditional
statement is the “body” of the subroutine. In this case, this subroutine is invoked from an invisible part of the
application.
Several important points should be noted.
- Lines 1 and 6
They mark the beginning and ending the subroutine. The word Private marks this subroutine being accessible
only from other VBA code of the form. The word Sub indicates this is a subroutine (as opposed to a function).
The name of the subroutine is Form_BeforeUpdate. Most importantly, Cancel As Integer indicates there is
a parameter. We’ll discuss this one later.
- Lines 2 to 5
They are the “body” of the loop. This code executes whenever the subroutine is invoked.
- Line 4
This line is special because it refers to Cancel. Cancel is a parameter as defined on line 1. This parameter
allows the subroutine pass information back to the caller. In this specific case, it allows the subroutine to tell
Access to cancel an update operation.