By default, Access displays records in the order of the primary key. If your table doesn't have a primary key, the records display in the order in which you enter them. Access uses sorting methods to display records in the desired order. If an index exists on the field in which you sort the records, the sorting process for large tables is quicker. Access automatically uses indexes, if indexes exist, to speed the sort in a process called query optimization. The following sections show how to use Access's sorting methods to display records in the sequence you want. The Customers table of Northwind.mdb is used for most examples in this chapter because it's typical of a table whose data you might want to sort. Note
Freezing Display of a Table FieldIf the table you're sorting contains more fields than you can display in Access's Table Datasheet view, you can freeze one or more fields to make viewing the sorted data easier. Freezing a field makes the field visible at all times, regardless of which other fields you display by manipulating the horizontal scroll bar. Note This example and those that follow use field names, rather than column header names (captions). The Microsoft developers added spaces when two nouns make up a field name, such as Company and Name, are extended and, in this case, superficial properties of fields. To freeze the CustomerID and CompanyName fields of the Customers table, follow these steps:
When you scroll to fields to the right of the frozen columns, your Datasheet view of the Customers table appears as shown in Figure 7.1. A solid vertical line replaces the half-tone gridline between the frozen and thawed (selectable) fields. Figure 7.1. The CustomerID and CompanyName fields of this Datasheet view of the Customers table are frozen.
Tip If you frequently freeze columns, add the Freeze Columns button from the Datasheet collection to your Datasheet toolbar.
To learn how to customize your toolbars, see "Customizable Toolbars," p. 574. Sorting Data on a Single FieldAccess provides an easy way to sort data in the Datasheet view, called a Quick Sort. Simply click the field name button of the field you want to use to sort the table's data and click either the Sort Ascending or the Sort Descending icon on the toolbar. In mailing lists, a standard practice in the United States is to sort the records in ascending ZIP code order. This practice often is also observed in other countries that use postal codes. To Quick Sort the Customers table in the order of the Postal Code field, follow these steps:
Sorting Data on Multiple FieldsAlthough the sort operation in the preceding section accomplishes exactly what you specify, the result is less than useful because of the variants of postal-code formats used in different countries. What's needed here is a multiple-field sort: first on the Country field and then on the PostalCode field. You can select the Country and the PostalCode fields to perform the multicolumn sort. The Quick Sort technique, however, automatically applies the sorting priority to the leftmost field you select, Postal Code. Access offers two methods of handling this problem: reorder the field display or specify the sort order in a Filter window. Follow these steps to use the reordering process:
Filters are discussed later in this chapter; see "Filtering Table Data", p. 249.
The sorted table, shown in Figure 7.3, now makes much more sense. A multiple-field sort on a table sometimes is called a composite sort. Figure 7.3. Rearrange the fields to use Quick Sort on multiple fields in left-to-right sequence. Changing the sequence of fields in Datasheet view affects the display but not the design of the table.
Removing a Table Sort Order and Thawing ColumnsAfter you freeze columns and apply sort orders to a table, you might want to return the table to its original condition. To do so, Access offers you the following choices:
If you make substantial changes to the layout of the table and apply a sort order, it's usually quicker to close and reopen the table. (Don't save your changes to the table layout.) |