Microsoft KB Archive/304564

= ACC2000: How to Create a Custom Sort Order =

Article ID: 304564

Article Last Modified on 6/25/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q304564



Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
This article shows you how to create a custom sort order by using the Switch function.

Sometimes, you may want Microsoft Access to sort based on a custom sort order (that is, not ascending or descending, but a different, user-specified sort order).

For example, the Northwind sample database contains an employees table in which an employee may have one of four possible titles. Access sorts these titles alphabetically as:


 * Inside Sales Coordinator
 * Sales Manager
 * Sales Representative
 * Vice President, Sales

However, you may want to sort these titles with a custom sort order, such as:


 * Vice President, Sales
 * Sales Manager
 * Inside Sales Coordinator
 * Sales Representative



MORE INFORMATION
To create a custom sort order on the Employees table in the Northwind Sample Database, follow these steps:  Start Microsoft Access and open the Northwind.mdb sample database. On the View menu, point to Database Objects, and then click Queries. Click New. Click Design View, and then click OK. In the Show Table dialog box, click the Employees table, and then click Close.  Add the following fields to the query grid by either double-clicking the field names, or by dragging them into the columns of the query grid: <pre class="fixed_text">  Query: qryCustomSort -  Type:  Select Query

Field: EmployeeID Table: Employees

Field: LastName Table: Employees

Field: FirstName Table: Employees

Field: Title Table: Employees

NOTE: In the following example, an underscore (_) is used as a  line-continuation character. Remove the underscore from the end of the line when re-creating the example.

Field: Customsort:Switch([title]=&quot;Vice President,Sales&quot;,&quot;a&quot;, _         [title]=&quot;Sales Manager&quot;,&quot;b&quot;, _          [title]=&quot;Inside Sales Coordinator&quot;,&quot;c&quot;, _          [title]=&quot;Sales Representative&quot;,&quot;d&quot;) Table: Employees Sort: Ascending Show: Unchecked </li> Save the query as qryCustomSort. Run the query.</li></ol>

Note that the employees are now sorted as follows:


 * Vice President, Sales
 * Sales Manager
 * Inside Sales Coordinator
 * Sales Representative

<div class="references_section">