Interaction Choose Method

Posted on 2006-03-21
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

    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

    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
    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.
    LVL 9

    Accepted Solution

    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 85

    Expert Comment

    by:Mike Tomlinson
    "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

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    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…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now