Lesson 3
Home Up Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Assignment 6

ASCII table

Assignment 3

Nested If Structures

There 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
    lblResponse.Caption = "You are too young to vote"
ElseIf  strGender = "M" Then   'Notice the ElseIf keyword
    lblResponse.Caption = "Sir, you are able to vote"
Else
    lblResponse.Caption = "Madam, you are able to vote"
End If

Nesting Structures

It 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 Functions

There are several built-in functions that give more flexibility to create sophisticated decision making.

IIf()

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", "")

Choose()

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.

IsEmpty()

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
Dim blnResult As Boolean
blnResult = IsEmpty(varVariable)    'blnResult would be assigned a true   
varVariable = "Hello World"
blnResult = IsEmpty(varVariable)    'blnResult would be assigned a false
varVariable = Empty
blnResult = IsEmpty(varVariable)    'blnResult would be assigned a true   
varVariable = Null
blnResult = IsEmpty(varVariable)    'blnResult would be assigned a false  

IsNull()

If the argument is an expression that contains no valid data, the function will return a true. For example:

Dim varVariable As Variant
Dim blnResult As Boolean
blnResult = IsNull(varVariable)    'blnResult would be assigned a false
varVariable = Null
blnResult = IsNull(varVariable)    'blnResult would be assigned a true
varVariable = Empty
blnResult = IsNull(varVariable)    'blnResult would be assigned a false

IsNumeric()

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!

VarType()

This function returns an integer value representing the type of the argument. The values and constants are shown in the chart below:

Value Constant Description
0 vbEmpty Empty
1 vbNull Null
2 vbInteger Integer value
3 vbLong Long Integer value
4 vbSingle Single-precision decimal value
5 vbDouble Double-precision decimal value
6 vbCurrency Currency value
7 vbDate Date value
8 vbString String
9 vbObject Object
10 vbError Error value
11 vbBoolean Boolean value
12 vbVariant Variant
13 vbDataObject a DAO
14 vbDecimal Decimal value
17 vbByte Byte value
8192 vbArray Array * this value is added to the type of the array

For example:

Dim varVariable As Variant
Dim intResult As Integer
varVariable = 21
intResult = VarType(varVariable)    'intResult would be assigned a 17 for a byte value
varVariable = "Tony"
intResult = VarType(varVariable)    'intResult would be assigned an 8 for a string value

Objects to use with decisions

There are a couple of controls that are available that work with the decision structures.

Checkboxes

Checkboxes are the controls that let you select options. None or more than one may be selected within a group. To add a checkbox to the form, double click the control wpe3.jpg (787 bytes) from the toolbox. The property that stores whether the control is checked is the value property. This is a Boolean property that is true if checked, false if unchecked.

Optionbuttons

Optionbuttons are similar to the Checkbox, however only one selected within a group. To add a optionbutton to the form, double click the control wpe4.jpg (852 bytes) from the toolbox. The property that stores whether the control is checked is the value property. This is a Boolean property that is true if checked, false if unchecked.