Daily Newbie - 04/28/2001
Although the Choose() Statement only returns a single value, it still evaluates each one. In effect,
it acts like a compact series of If...Then statements. This can result in the sometimes baffling behavior
of displaying one message box with the correct value and many empty ones. For this reason, the results
of a Choose() statement should be returned to a variable before displaying it in a message box.
“To Start Things
The initial plan for the Daily Newbie was to cover each function VB has to offer
in alphabetical order. I have now modified this plan slightly to skip over some of
the more advanced (or tedious) commands that I don't think the Newbie would benefit from.
Thanks again all who have written in support of this effort. It makes a difference.
Today's command is not widely known for some reason, but is faily useful.
I have been guilty of writing functions that do the exact same thing several times. I think you will
like this one.
course) – “(1) : to make a selection"
- Webster's online
Making a choice between several possible options.
VB Help Description: Selects and returns a value from a list ofarguments.
English: Returns the option associated with the value passed it (I will just have to show you!)
Syntax: Choose(index, Choice1, Choice2, etc...)
Usage: strDecision = Choose(intChoice , "Just do it" , "Don't do it" , "It's your life" )
Copy & Paste Code:
Today's code snippet will prompt for a month number and return a string
that corresponds to it.
Dim strMonth As String
Choice = Val(InputBox("Enter a Number (1-12):"))
If Choice + 0 = 0 Then Exit Do
strMonth = Choose(Choice, "Jan", "Feb", "Mar", "Apr", _
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
I really like the Choose function. It comes in handy for with evaluating which option button
was clicked, or anything else that returns an index number. Unfortunatly, like the Array() command
covered a couple of articles ago, the Choose() function requires a seperate hard coded value for
each possible choice. This isn't neccesarily a bad thing, but I am allergic to hard coding, so it
just rubs me wrong. I guess the chances of the order of the months changing is pretty slim...
Things to watch out for:
If the Index value passed in is null, an error will result. Therefore, if you are using a variant
as your Index, you should add zero to it to initialize it as a number. This will make the default value zero, not null.
Tomorrow's Keyword: Chr()