Learn how to a build a cloud-first strategyRegister Now


Interaction Choose Method

Posted on 2006-03-21
Medium Priority
Last Modified: 2012-08-13
Could someone give me an example of how to use the choose method in vb?

Question by:GoldenJag

Author Comment

ID: 16252286
Better yet, i was looking for a way to have a user choose between two options.  I was wondering if there is a way to do this without creating another form and putting two radio buttons on it...etc.

is that what the choose method does?
LVL 15

Assisted Solution

Computron earned 1000 total points
ID: 16252337
Choose works like this

Dim MyString as String, strIndex as String

strIndex = 1
MyString = Choose(strIndex,"String1","String2")
Debug.Print MyString would show String1
If strIndex was 2, MyString would be equal to String2
The index in choose starts at 1
LVL 44

Expert Comment

by:Leigh Purvis
ID: 16253863
You'll still need a method of determining the arguments for the choose function.
(Bar in mind that every argument is evaluated for it).

For options that might change a combo or listbox is a bit more normal.

Accepted Solution

Naveen Swamy earned 1000 total points
ID: 16254414
In Access, the Choose function returns a value from a list of values based on a given position.

The syntax for the Choose function is:

Choose ( position, value1, value2, ... value_n )

position is position number in the list of values to return.

value1, value2, ... value_n is a list of values.


If position is less than 1, the Choose function will return a null value.

If position is greater than the number of values, the Choose function will return a null value.

If position is a fraction (not an integer value), it will be rounded to the nearest whole number.

For example:

Choose(1, "Tech", "on", "the", "Net") would return "Tech"
Choose(2, "Tech", "on", "the", "Net") would return "on"
Choose(3, "Tech", "on", "the", "Net") would return "the"
Choose(4, "Tech", "on", "the", "Net") would return "Net"
Choose(5, "Tech", "on", "the", "Net") would return NULL
Choose(3.75, "Tech", "on", "the", "Net") would return "the"

VBA Code
The Choose function can be used in VBA code. For example:

Dim LValue As Boolean

LValue = Choose(1, "Tech", "on", "the", "Net")

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16254749
"i was looking for a way to have a user choose between two options."

You could use the MsgBox() function and have the user select Yes/No to indicate their preference for the first/second option:

Private Sub Command1_Click()
    Dim msg As String
    msg = "How often do you like your system to crash?" & vbCrLf & vbCrLf & _
        "(1) Just before I click the Save button on important documents." & vbCrLf & _
        "(2) Just before I hit Print on the bosses reports." & vbCrLf & vbCrLf & _
        "Select YES for (1) ... or ... NO for (2)"
    Dim result As Integer
    result = MsgBox(msg, vbYesNo, "Crash Preference")
    Select Case result
        Case vbYes
            MsgBox "You selected Option 1"
        Case vbNo
            MsgBox "You selected Option 2"
    End Select
End Sub

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question