Access XP: Sorting Records

Lesson 12: Sorting Records

/en/accessxp/editing-form-records/content/

Introduction

By the end of this lesson, you should be able to:

  • Review table records and form records concepts
  • Perform a simple sort
  • Perform a multi-field sort
  • Remove a sort
  • Save a sort

Sorting records to find information

Sorting is one method that helps you find information quickly and easily in a database. Records can be sorted in ascending order (A-Z) and descending order (Z-A). You can sort by zip code, last name, first name, company, contact type (e.g., family, friend, relative), etc. You can even sort by contact type and then by last name within each contact type.

It's easiest to see the results of a sort if you work in Datasheet view.

To sort records:

  • Open the desired form or table (e.g., Contacts table).
  • Choose ViewDatasheet View from the menu bar if Datasheet view is not already displayed.

    Records Shown in Datasheet View
  • Click anywhere in the column you want to sort by.
  • Click the Ascending or Descending button on the toolbar (or choose RecordsSortSort Ascending or Sort Descending from the menu bar).

    Records Sorted in Descending Order by Last Name

To sort by Form view, switch to Form view, select the field you want to sort by, and click the Sort button on the toolbar. To see the results, use the status area to move through the records.

Performing a multi-field sort

Multi-field sorting allows you to define multiple fields in your sort. You specify the column to sort by, (e.g., Last Name), and within that column sort by some other specified field (e.g., First Name). For example, you could sort all of the Brown records by first name.

To sort by multiple fields:

  • Open the desired form or table in Datasheet view.
  • Move the columns to be included in the sort so they are positioned next to each other.

Moving a column in Access is similar to moving a column in Excel. Click in the column heading of one field to highlight the entire column. Then drag the entire column so it sits alongside the other column(s) to be used in the sort.

  • The column to be sorted first should be positioned to the left of the second column to be sorted, and so on.

    Column To Be Included in Sort Moved to New Position
  • Select all fields (columns) involved in the multiple sort process.

    Columns Selected in Multi-Field Sort
  • Click the Ascending or Descending button on the toolbar to sort the records (or choose RecordsSortSort Ascending or Sort Descending from the menu bar).

    Records Displayedin New Sort Order

Multiple field sorts cannot be performed in Columnar or Tabular Form view.

Removing a sort

After a sort is performed, you will be given the option to save the sort or to remove it to allow the records to return to the way they were ordered before you applied the sort.

To remove the sort order:

  • Choose RecordsRemove Filter/Sort from the menu bar.

    Remove Filter/Sort option on Records Menu

    The records revert to their ordering before the sort was applied.

If you add new records to a table that has been sorted, any new records are automatically saved.

Saving a sort

When you open a table in Access, the records are displayed in order based on values that appear in the primary key field. In other words, Access defaults to a sort on the primary key when a table is opened. However, a sort can be performed against any field listed in a table. Sorting table records actually changes the table design. When you attempt to close a table after a sort, Access will prompt you to save the changes to the table design.

To save a sort:

  • Exit the table.
  • Click the Yes button in response to the question, Do you want to save changes to the table?

    Confirm Save Dialog Box

    The sort order is saved. When you open the table again, the records will still be sorted.

To cancel a sort:

  • Exit the table.
  • Click the No button in response to the question, Do you want to save changes to the table?

    The change is not saved; the table remains in its original design.

Challenge!

  • Open the Contacts table in Datasheet view.
  • Perform the following single-field sorts:
    • Sort (descending or ascending) the records by the Last Name field.
    • Sort (descending or ascending) the records by the State/Province field.
    • Sort (descending or ascending) the records by the Contact ID field.
  • Perform the following multi-field sorts:
    • Move the City field to the right of the State field and sort (descending or ascending) the records by State and City.
    • Move the City field to the right of the Last Name field and sort (descending or ascending) the records by Last Name and City.
  • Remove the last sort performed.
  • Cancel the sort when you close the Contacts table, and click the No button in response to the Do you want to save changes to the design of table 'Contacts'? system prompt.

/en/accessxp/finding-records/content/