Microsoft Office Tutorials and References
In Depth Information
Making Decisions in VBA Code
Making Decisions in VBA Code
Decision-making is a big part of programming, because most programs need to
be smart enough to figure out what to do in various circumstances. Often, you
want your code to do one thing if such-and-such is true and to do something
else if such-and-such is false. You use conditional expressions to determine
whether something is true or false. A conditional expression is one that
generally follows the syntax
Value ComparisonOperator Value
where Value is some chunk of information and the ComparisonOperator
is one of those listed in Table 3-2.
Table 3-2
Comparison Operators
Equal to
Less than
Less than or equal to
Greater than
Greater than or equal to
Not equal to
The expression
[Last Name] = “Smith”
compares the contents of the Last Name field with the string “Smith”. If
the [Last Name] field does, indeed, contain the name Smith, the
expression is (or returns) True. If the [Last Name] field contains anything other
than Smith, the expression is (returns) False.
Chapter 3
Another example is the following statement:
[Qty] >= 10
The content of the Qty field is compared with the number 10. If the number
stored in the Qty field is 10 or greater, the expression returns True. If the
number stored in the Qty field is less than 10, the expression returns False.
You can combine multiple conditional expressions into one by using the
logical operators summarized in Table 3-3.
Search JabSto ::

Custom Search