VB6 Tutorial 27: Other Control Flow Functions

Posted On 2018-04-11 by VB6Boy
Tags: VB6 Tutorials Windows
Views: 31

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

This section discusses the control flow functions such as IIf, Choose and Switch functions.

You can choose to use these VBA control flow functions instead of always using the common control flow statements or control flow blocks like If-Else or Select Case.

See the following lessons for If-Else and Select Case statements:

Why to use control flow functions instead of the control flow blocks?

The IIf, Choose and Switch control flow functions reduce the amount of code you have to write. In many cases, just one line of code does your work, while the If or Select Case control flow blocks takes more lines.

The IIF function

You can use the IIf function instead of the If-Else statements.

Remember that the IIF function can never be the substitute for all the conditional If-Else problems. In fact, the If-blocks are very powerful in the sense that they can solve many complex problems. All the same the IIf function is useful especially when the problem is small.


Value = IIf(Condition, True Part, False Part)

Example of IIF

str = IIf(n > 0, "Greater than zero", "Less than Zero")

Equivalent code

If n > 0 Then
    str = "Greater than Zero"
    str = "Less than zero"
End If

So now this is clear that the IIf function does the same job of an If-Else block having five lines with just one line of code.

The Choose function

The choose function selects and returns a value from a list of arguments.


m = Choose(2, "White", "Green", "Yello")

The index determines which argument value to be returned. Here the index is 2, so the string value "Green" is returned and assigned to m.

The Switch function

You can get the same effect of a Select Case block using the Switch function with just one line of code.


size = Switch(num <= 9, 1, num <= 99, 2, num <= 999, 3)

Equivalent code

Select Case num
    Case Is <= 9
        size = 1
    Case 10 To 99
        size = 2
    Case 100 To 999
        size = 3
End Select

So the same result is achieved using the Switch function. But the Switch function evaluates all the expressions, even though only one is True, and that slows down the process.

Select Case block with Else

Select Case Color
    Case vbRed
        str = "Red"
    Case vbBlue
        str = "Blue"
    Case vbBlack
        str = "Black"
    Case Else
        str = "Other"
End Select

Equivalent code

str = Switch(Color = vbRed, "Red", Color = vbBlue, "Blue", Color = vbBlack, "Black", True, "Other")

Here the True argument indicates the else part.


The control flow functions IIf, Switch and Choose are always slower than the If or Select Case structure and this is a big disadvantage of them. So always be aware that though these functions reduce the amount of code, they may slow down your application.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

About the Author

VB6Boy has posted a total of 71 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links