Data Analysis: Sorting Records |
|
Introduction |
One of the actions you can take consists of rearranging the list of records in an order other than the one in which they were entered. For example, a user enters the list of students in the order they arrive. At one time the user may want to see a list of students in alphabetical order based on they last names. Rearranging a list of records is referred to as sorting. Microsoft Access provides the means of sorting records on all database objects, including tables, queries, and forms. To sort the records of a table, a query, or a form, you can right-click the column or the control that displays its values and click Sort Ascending. This would sort the records based on the field you selected. When a list is sorted in ascending order: |
To sort the records in the reverse order, you can right-click a column (table or query) or a control (form) and click Sort Descending. After sorting records, if you want to perform another operation, you may need to reset the list to its original arrangement. To do this, you can right-click the object and click Remove Filter/Sort. You can also access these three options on the main menu under Records. They are also available on the toolbar(s). After sorting the records, you can close the object but you would be asked whether you want to save the arrangement. If you click Yes, the object would be saved with the new arrangement and you can view it next time. If you are working in a query, to include the arrangement in it, open the query in the Design View. In the lower section of the window, click the Ascending field of the desired column and select Ascending from its combo box. In the same way, you can sort the field in reverse order by selecting the Descending option. To programmatically sort records on a table, a query, or a form, etc, call its OrderBy() method and pass it the name of the column on which the sorting would be based. After calling OrderBy(), access its OrderByOn Boolean property and set its value to True. To remove the sorting, access the OrderByOn and set its value to False.
If you are visually creating a query using the Select Query window, to sort the records, after selecting the column on which the sorting would be based, click the box corresponding to its Sort row, click the arrow of its combo box and select Ascending: To sort the records in reverse order, select Descending instead.
In the SQL, to sort a field in ascending order, you can include the ORDER BY clause in your statement. The syntax used would be: SELECT What FROM WhatObject ORDER BY WhatField; The field used as the basis must be recognized as part of the selected columns. Imagine you have created a list of staff members made of their first and last names in a table named StaffMembers. If you want to order the list in alphabetical order based on the LastName column, you would use a statement such as: SELECT FirstName, LastName FROM StaffMembers ORDER BY LastName; If you use the * operator to include all fields, you can order the list based on any of the table's fields, as we learned during data analysis. Imagine that you have created a query that includes all fields. The following statement would list the records of the StaffMembers table based on the alphabetical order of the LastName column: SELECT * FROM StaffMembers ORDER BY LastName; By default, records are ordered in ascending order. Nevertheless, the ascending order is controlled using the ASC keyword specified after the based field. For example, to sort the last names in ascending order of a query that includes the first and last names, the above statement can also be written as follows: SELECT FirstName, LastName FROM StaffMembers ORDER BY LastName ASC; The second statement can be written as: SELECT * FROM StaffMembers ORDER BY LastName ASC; If you want to sort records in descending order, use the DESC keyword instead. It produces the opposite result to the ASC effect. To sort records in reverse alphabetical order, the above two statements can be written as: SELECT FirstName, LastName FROM StaffMembers ORDER BY LastName DESC; The second statement can be written as: SELECT * FROM StaffMembers ORDER BY LastName DESC; If you want to programmatically create a query from one of these statements, remember that you can use the CreateQueryDef() method. |
Practical Learning: Sorting Records on a Form |
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|