Often actions are controlled based on multiple conditions. For
example
Commission rate is .03 if the salesperson sells more than $300,000 worth of
product and they have been employed more than 4 years. Otherwise it is .02.
If decSales > 300000
If intYearsEmployed > 4 then
decCommission
=decSales * .03
Else
decCommission
= decSales * .02
End If
Else
decCommission = decSales * .02
End If
Commission rate is .03 if the salesperson sells more than $300,000 worth of product or they have been employed more than 4 years. Otherwise it is .02.
If decSales > 300000
decCommission =decSales * .03
ElseIf intYearsEmployed > 4 then
decCommission =decSales * .03
Else
decCommission = decSales * .02
End If
Commission rate is .03 for salespersons who sell more than $300,000 worth of product and are not considered management.
decCommission = 0
If decSales > 300000
If blnManagement
'Intentionally left empty
Else
decCommission
= decSales * .03
End If
End If
Another way to make statements conditional based on more than one condition is to use compound If statements. Compound If statements use logical operators – And, Or, Not.
If (condition) And (condition) then
statement
statement
statement
end if
When using the logical operator And All conditions stated must be true for the
condition to be true.
For example
If decSales > 300000 And intYearsEmployed > 4 then
decCommission = decSales * .03
Else
decCommission = decSales * .02
End If
One way to illustrate logical operator is with a Venn Diagram
Sales > $300.000 And Years employed > 4
AND - All conditions are present.
Another way to resolve a condition with logical operators is with a truth table
|
Truth Table for Conjunctions |
||
|
1st Condition |
2nd Condition |
Statement |
|
A |
B |
A and B |
|
true |
true |
true |
|
true |
false |
false |
|
false |
true |
false |
|
false |
false |
false |
Another example
If decYearsEmployed > 4 _
And decSales > 300000 _
And decPreviousSales > 300000 then
decCommission
= decSales * .03
Else
decCommission = decSales * .02
End If
Years employed > 4
And Sales > $300.000
And Previous Sales > $300,000
AND - All conditions are present.
|
Truth Table for Conjunctions |
||||
|
1st Condition |
2nd Condition |
3rd Condition |
Statement |
|
|
A |
B |
C |
A and B and C |
|
|
true |
true |
true |
true |
|
|
true |
true |
false |
false |
|
|
true |
false |
true |
false |
|
|
true |
false |
false |
false |
|
|
false |
true |
true |
false |
|
|
false |
false |
true |
false |
|
|
false |
true |
false |
false |
|
|
False |
False |
false |
false |
|
Note: When constructing compound conditions in Visual Basic you may not have implied operands.
If sngSales > 100000 And < 300000 then
sngCommission = sngSales * .03
End If
The previous error message was displayed when the cursor rested on the first condition and also when pointing to the key word Then. A different message is displayed when the cursor rests on the conditional statement.
Both errors occur because of the missing operand decSales in
the second condition.
You must repeat the operand
If decSales > 100000 And decSales < 300000 Then
decCommission = decSales * 0.1
End If