Sort Property Example (VC++)

This example uses the Sort property to set the sort order of a dynaset-type Recordset based on an Orders table. The records in rstSorted will be in ascending Ship Country order.

CdbDBEngine      dbeng;
CdbDatabase      dbsNorthwind;
CdbRecordset   rstOrders;
CdbRecordset   rstSorted;

dbsNorthwind = dbeng.OpenDatabase(_T("Northwind.mdb"));
rstOrders = dbsNorthwind.OpenRecordset(_T("Orders"), dbOpenDynaset);
// set sort order
rstOrders.SetSort(_T("ShipCountry"));
// create 2nd recordset
rstSorted = rstOrders.OpenRecordset();

// process...

rstSorted.Close();
rstOrders.Close();
dbsNorthwind.Close();

Note In some situations, it may be more efficient to create the second Recordset object with the conditions you want in one step. As a general rule, when you know the data you want to select, it's usually more efficient to create a Recordset with an SQL statement. This example shows how you can create just one Recordset and obtain the same results as in the preceding example.

CdbDBEngine      dbeng;
CdbDatabase      dbsNorthwind;
CdbRecordset      rstOrders;
   
dbsNorthwind = dbeng.OpenDatabase(_T("Northwind.mdb"));
rstOrders = dbsNorthwind.OpenRecordset(_T("SELECT * FROM Orders   ORDER BY ShipCountry;"));
// process...

rstOrders.Close();
dbsNorthwind.Close();