Sorting Records

You can use the recordset's Sort property to change the order in which records are displayed. The Sort property does not physically rearrange the records; it merely displays the records in the order specified by the index. If you are sorting on non-indexed fields, a temporary index is created for each field specified in the index. This index is removed automatically when you set the Sort property to an empty string. You can only use Sort on client-side cursors. If you use the server-side cursor, you will receive this error:

The operation requested by the application is not supported by the provider

The default sort order is ascending. To order a recordset by country in ascending order, then by city in descending order, you would use the following statement:

rst.Sort = "Country ASC, City DESC"

Although you can use the Sort property to sort your data, you will most likely get better performance by specifying an SQL ORDER BY clause in the SQL statement or query used to open the recordset.

The procedure in Hands-On 14-11 displays customer records from the Northwind database in ascending order by country.

0 0

Post a comment