ACC2000: How to Create a Custom Sort Order (304564)



The information in this article applies to:

  • Microsoft Access 2000

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:
  1. Start Microsoft Access and open the Northwind.mdb sample database.
  2. On the View menu, point to Database Objects, and then click Queries.
  3. Click New.
  4. Click Design View, and then click OK.
  5. In the Show Table dialog box, click the Employees table, and then click Close.
  6. 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:
       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]="Vice President,Sales","a", _
              [title]="Sales Manager","b", _
              [title]="Inside Sales Coordinator","c", _
              [title]="Sales Representative","d") 
       Table: Employees
       Sort:  Ascending
       Show:  Unchecked
    					
  7. Save the query as qryCustomSort. Run the query.
Note that the employees are now sorted as follows:
  • Vice President, Sales
  • Sales Manager
  • Inside Sales Coordinator
  • Sales Representative

REFERENCES

For more information about the Switch function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type switch function in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:6/25/2004
Keywords:kbhowto KB304564