Home  Fees/Services  Microsoft Access Templates  Tutorials  Access Download  Articles  Search  Contact  Privacy  Links

Access Query Examples
Query Tutorials in SQL
SQL Choose Function Example

Choose Command Tutorial Microsoft Access 2007 Query Help
Access SQL Tutorials




MS SQL Tutorials:
Query Too Complex
Aggregate Functions
Choose Function
SQL Not Operator
More Choose Functions
Crosstab Query Tutorial
Crosstab Query Advanced
DateTime Query
Delete Query Tutorial
External Link Query
Access Report Filter Query
Group By Clause
Access Having Clause
Histogram Query Tutorial
Insert Into SQL Query
Master/Detail Updates
Order By Dynamic
Access Parameter Query
Predicate Query Example
Self Join Query Tutorial
Scalar Query Example
Child/Parent Table Query
Select Statement Tutorial
Select Top 1 Query
Union Query Example
Update Query

Multi-Select Parameter Forms Query Tutorial


Choose Command in Microsoft Access

The choose command is similar to the Decode function in Oracle.  Assume the same setup as the Dynamic Order By Clause example but add a 3rd option for sorting by Address.

See below how the choose function expands your capabilities.

Select M_Employees.Name, M_Employees.Emp_Number,
M_Employees.Address
From M_Employees
Order by
Choose(Val(Forms!F_Emp!Sort_Option),M_Employees.Emp_Name,
M_Employees.Emp_Number, M_Employees.Address);

Note:  Entry in Forms!F_Emp!Sort_Option must be an integer and it is best to force it to a number with the Val() function.  You can have up to 29 options and you can use the Choose Function in each part of the query except the From clause.

New! Download Access SQL Choose Command Demonstration Database.

The choose function can be used in each part of a query, except possible the From clause.

2nd Choose Example

My boss wants an employee salary report and on odd days of the month he wants the data to show by last name then first name.  On the even days of the month he wants the data the other way, first name followed by last name.

Just in case you ever have a boss like that - here is the solution:

Create a parameter form for the report.  Here we have created Form1.  Then create the SQL query text using the choose function as shown:

select Choose statement

Below you'll see that when we change the choice field to 2 the last name appears first followed by the first name:

Choose Command, Decode Command

The slight trick to this solution is that we use two Choose function statements in each query.  The first choose is used for the Name1 field and the second choose is used for the Name2 field.

Additional Choose Command Information:
 

DoCmd OpenReport in Microsoft Access

crit = "Flight_ID" & Choose(get_global("flight_type"), ">-1", "=0", "<>0") output: On Error GoTo no_data Select Case Me.Select_Report Case 1 DoCmd. ...
www.blueclaw-db.com/docmd_openreport.htm






Have errors?  No doubt we can answer your question quickly and easily.  Simple SQL questions get simple answers at no charge via email.





Popular Database Templates:


CRM Software Templates

Order Fullfillment Database

Order Processing Pre-built Software Template


Contact Information

Microsoft Access Help

Microsoft Access 2007 Query Tutorial

Free SQL Tutorial & Query Solutions Workaround Runtime Examples Samples