Chapter 13. Control Statements
We conclude our discussion of the VBA language with a discussion of the main VBA control statements, which are statements that affect the flow of control (or flow of execution) in a program.
The If...Then Statement
The If...Then
statement is used for conditional
control. The syntax is:
IfConditionThen ' statements go here . . . ElseIfAnotherConditionThen ' more statements go here . . . Else ' more statements go here . . . End If
Note that we may include more than one ElseIf
part, and that both the
ElseIf part(s) and the
Else part are optional. We can also squeeze all
parts of this statement onto a single line, which is generally only a
good idea when the ElseIf and
Else parts are not required.
To illustrate, the following code checks to see if the
FirstName field is null. If so, it replaces the
Null value with a question mark. If not, it
capitalizes the first name.
rs.Edit If IsNull(rs!FirstName) Then rs!FirstName = "?" Else rs!FirstName = UCase(rs!FirstName) End If rs.Update
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access