|
Nested If StructuresThere may be a need have more than two outcomes, based on two separate conditions. This need may not be solved using a Case because of different variables that may be required A form of coding called Nesting Ifs may be required. For example, a test to see if someone is old enough to vote might also need a test for their gender. A nested If structure may look like the following example: If bytAge < 18 Then Nesting StructuresIt is also possible to nest Select Case statements within If statements or vice versa. Select Case statements can contain other Select Case statements. It all depends on your needs. The main consideration when nesting statements is to include the entire internal structure within one logical group of the external structure. Logical FunctionsThere are several built-in functions that give more flexibility to create sophisticated decision making.
This function enables you to make a simple decision in one line of code! The condition, true outcome and the false outcome are separated with commas. ResultOfSameTypeAsOutcome = IIf(condition,OutcomeIfTrue,OutcomeIfFalse) For example, if you wanted your program to include plurals if more than one pencil was on hand, the following code would accomplish this. lblPencils.Caption = "Pencil" & IIf(intNumPencils > 1, "s", "")
This function lets you create a list of outcomes to return. The value of the index determines which item in the list is returned. ResultOfSameTypeAsOutcome = Choose(bytIndex,Outcome1,Outcome2,....,OutcomeN) For example, if you wanted to return the name of a song on a track of a CD, the following code would accomplish this. strSongName = Choose(bytTrack,"Blue Suede Shoes","Blueberry Hill","Blue Moon") *Note* If the index value is 0 or larger than the number of outcomes, a Null value is returned.
If the argument is a variable that has not been initialized, or has been set to Empty the function will return a true. For example: Dim varVariable As Variant
If the argument is an expression that contains no valid data, the function will return a true. For example: Dim varVariable As Variant
If the argument can be evaluated as a number the function will return a true. For example: Dim varVariable As Variant Dim blnResult As Boolean varVariable = 21 blnResult = IsNumeric(varVariable) 'blnResult would be assigned a true varVariable = -34.567 blnResult = IsNumeric(varVariable) 'blnResult would be assigned a true varVariable = "(613)389-8932" blnResult = IsNumeric(varVariable) 'blnResult would be assigned a false, phone numbers are not valid numbers!
This function returns an integer value representing the type of the argument. The values and constants are shown in the chart below:
For example: Dim varVariable As Variant Objects to use with decisionsThere are a couple of controls that are available that work with the decision structures.
|