Skip to content
Home ยป VB 6 – Choose() Function

VB 6 – Choose() Function

    The VB 6 Choose() function is another function that let you choose values based on an index. However, do not confuse it to be an array. The Choose () function is very useful in menu based program where you can display a menu and let user input there choice.

    When the user makes a choice, the value corresponding to the choice is returned and stored in another variable. Let us see the syntax for Choose () function.

    Dim Variable As String
    Variable = Choose(indexNo, "Value1", "Value2", "Value")

    The value in the above program could be numbers. We have chosen the string type to demonstrate the usage.

    Example Program: Choose() Function

    In this example, we will read an input number and based on the input, the program will find the name of the student and display it on the screen.

    Private Sub Command1_Click()
    Dim indexNo As Integer
    Dim Student As String
    
    indexNo = -1
    
    indexNo = InputBox("Enter a Number between 1 - 5, Enter 0 to end")
    
    If indexNo >= 1 Then
    
    Student = Choose(indexNo, "Peter", "Ram", "Mary", "Abdul", "Lee")
    
    End If
    
    If indexNo = 0 Then
    
    End
    
    End If
    
    MsgBox ("Student Name is" & " " & Student)
    
    End Sub

    User Input

    Input - Choose() Function
    Figure 1 – Input – Choose () Function

    Output: Choose () Function

    The output of the program is the name of the student whose index number matches in the Choose() function.

    Output - Choose Function
    Figure 2 – Output – Choose Function