Microsoft Office Tutorials and References
In Depth Information
Making Decisions in VBA Code
Figure 3-2:
Basic idea
behind
If...
End If .
You have a little bit of flexibility in using If...End If. If only one line of code
executes for a True result and only one line executes for a False result, you
can put the whole statement on a single line and omit the End If statement,
as follows:
If State=”NY” Then TaxRate=0.075 Else TaxRate=0
Because you can use any built-in function in VBA, and because Access
supports the use of the IIf() (Immediate If) function, you can also write the
preceding statement as an expression:
TaxRate = IIf([State]=”NY”,0.075,0)
In the block format, you can also write code that tests for more than just two
possible conditions by using the optional ElseIf statement. Suppose that
the Reply variable stores a string of text. If Reply contains the word “Yes”,
your code does one thing. If Reply contains “No”, your code does something
else. If Reply contains neither “Yes” nor “No”, you want your code to do
something else instead. You could set up an If...End If block to test for
and respond to all three conditions, as follows:
Book VIII
Chapter 3
If Reply = “Yes” Then
statements for “Yes” reply
ElseIf Reply=”No” Then
statements for “No” reply
Else
statements for any other reply
End If
When the code has to choose a decision from among many possibilities, you
may find that using a Select Case...End Select block is easier. For
details, see “Using a Select Case block,” later in this chapter.
Search JabSto ::




Custom Search