Returns a value from a list of arguments.
Syntax
Choose(index, choice-1 [, choice-2] ... [, choice-n])
The Choose function has these arguments:
| Argument | Description |
|---|---|
index |
Required. A numeric expression or field that results in a value between 1 and the number of available choices. |
choice |
Required. A variant expression that contains one of the possible choices. |
Remarks
Choose returns a value from the list of choices based on the value of index. If index is 1, Choose returns the first choice in the list. If index is 2, it returns the second choice, and so on.
You can use Choose to look up a value in a list of possibilities. For example, if index evaluates to 3 and choice-1 = "one", choice-2 = "two", and choice-3 = "three", Choose returns "three". This behavior is useful when index represents the value in an option group.
Choose evaluates every choice in the list, even though it returns only one. Because of this behavior, watch for unwanted side effects. For example, if you use the MsgBox function as part of an expression in all the choices, Access displays a message box for each choice as it evaluates it, even though Choose returns the value of only one choice.
The Choose function returns Null if index is less than 1 or greater than the number of choices listed.
If index isn't a whole number, Access rounds it to the nearest whole number before it evaluates it.
Query examples
| Expression | Results |
|---|---|
SELECT Choose(3,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") AS Expr1 FROM ProductSales; |
Returns the third value from the list of values. Result: "Mar". |
SELECT DateofSale, Choose(Month(DateofSale),"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") AS MonthName FROM ProductSales; |
Returns the values in DateofSale and the corresponding month name in the MonthName column. Month(DateofSale) returns the month number for DateofSale, and Choose uses that number as the index for the list of choices. |
VBA example
Note
The following examples show how to use this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search, and then enter one or more terms in the search box.
This example uses the Choose function to display a name for an index that the procedure passes in the Ind parameter.
Function GetChoice(Ind As Integer)
GetChoice = Choose(Ind, "Speedy", "United", "Federal")
End Function