As another alternative to an If...Then condition, the Visual Basic language provides a function named Switch. Its syntax is: Public Function Switch( _
ByVal ParamArray VarExpr() As Variant _
) As Variant
This function takes one required argument. To use it in an If...Then scenario, pass the argument as follows: Switch(ConditionToCheck, Statement) In the ConditionToCheck placeholder, pass a Boolean expression that can be evaluated to True or False. If that condition is true, the second argument would be executed. When the Switch() function has been called, it produces a value of type Variant (such as a string) that you can use as you see fit. For example, you can store it in a variable. Here is an example: Private Sub cmdFunction_Click()
Dim Status As Integer, EmploymentStatus As String
Status = 1
EmploymentStatus = "Unknown"
EmploymentStatus = Switch(Status = 1, "Full Time")
MsgBox "Employment Status: " & EmploymentStatus
End Sub
This would produce:
In this example, we used a number as argument. You can also use another type of value, such as an enumeration. When using the Switch function, if you call it with a value that is not checked by the first argument, the function produces an error. To apply this function to an If...Then...Else scenario, you can call it using the following formula: Switch(Condition1ToCheck, Statement1, Condition2ToCheck, Statement2) In the Condition1ToCheck placeholder, pass a Boolean expression that can be evaluated to True or False. If that condition is true, the second argument would be executed. To provide an alternative to the first condition, pass another condition as Condition2ToCheck. If the Condition2ToCheck is true, then Statement2 would be executed. Once gain, remember that you can get the value returned by the Switch() function and use it.
To assist you with counting the items of a list, the Visual Basic language provides the For...Next loop. The syntax used is: For Counter = Start To End Statement(s) Next Used for counting, the For...Next loop begins counting at the Start point. Then it examines whether the current value (after starting to count) is greater than End; if that's the case, the program exits the loop. It then executes the Statement or Statements. Next, it increments the value of Counter by 1 and examines the condition again. This process goes on until Counter = End. The syntax above will increment the counting by 1 at the end of each statement. If you want to control how the incrementing processes, you can set your own, using the Step option. Here is the syntax you would use: For Counter = Start To End Step Increment Statement(s) Next Counter You can set the incrementing value to your choice. If the value of Increment is positive, the Counter will be added its value. This means that you can give it a negative value, in which case the Counter would be subtracted the set value.
Since the For...Next loop is used to execute a group of statements based on the current result of the loop counting from Start to End, an alternative is to state various steps in the loop and execute a group of statements for each one of the elements in the group. This is mostly used when dealing with a collection of items. The syntax of a For...Each statement is: For Each Element In Group
Statement(s)
Next Element
The loop will execute the Statement or Statement(s) for each Element in the Group.
Loops are used to repeat an action and they use the Do keyword in combination with other keywords to perform and conditional statement. There are various variations of the Do loops. The syntax of the Do While loop is: Do While Condition
Statement(s)
Loop
The program will first test the Condition. If the Condition is true, the program would execute the Statement or Statements and go back to the Do While statement and test the condition again. This expression will execute the Statement or statements AS LONG AS the Condition is true, as many times as the Condition will be visited and found true. If the Condition is false, the program will skip the Do While statement and not execute any. Here is an example: Private Sub cmdCounter_Click()
Dim Number As Integer
Do While Number < 46
MsgBox CStr(Number)
Number = Number + 4
Loop
MsgBox "Counting Stopped at " & CStr(Number)
End Sub
Since the Do While statement tests the Condition first before executing the Statement, sometimes you will want the program to execute the Statement first, then go back and test the Condition. Visual Basic offers a reverse to the syntax, which is: Do Statement(s) Loop While Condition In this case, Visual Basic will execute the Statement or Statements first, then it will test the Condition. If the Condition is true, the program will execute the Statement again. The program will continue this examination-execution as long as the Condition is true. The big difference here is that even if the Condition is false, the program will have executed the Condition at least once. Here is an example: Private Sub cmdCounter_Click()
Dim Answer As String
Do
Answer = CStr(InputBox("Are we there yet (1=Yes/0=No)?", "Counter", "1"))
Loop While Answer <> "1"
MsgBox "Wonderful, we have arrived"
End Sub
Here is an example of running the code:
An alternative to the Do While loop is the Do Until loop. Its syntax is: Do Until Condition
Statement(s)
Loop
This loop will first examine the Condition, instead of examining whether the Condition is true, it will test whether the Condition is false. Here is an example: Private Sub cmdCounter_Click()
Dim Answer As String
Do Until (Answer = "1")
Answer = InputBox("Are we there yet (1=Yes/0=No)?", "Counter", "1")
Loop
MsgBox "Wonderful, we have arrived"
End Sub
The other side of the Do Until loop would execute the Statement first, then it would examine the Condition. The syntax used is: Do
Statement(s)
Loop Until Condition
|
|
|||||||||||||||||||||
|
|