Filter Property Example (VC++)

This example creates a dynaset-type Recordset that contains only records for orders shipped to the United Kingdom.

CdbDBEngine      dbeng;
CdbDatabase      dbsNorthwind;
CdbRecordset   rstOrders; 
CdbRecordset   rstFiltered;

dbsNorthwind = dbeng.OpenDatabase(_T("Northwind.mdb"));

rstOrders = dbsNorthwind.OpenRecordset("Orders", dbOpenDynaset);
rstOrders.SetFilter(_T("ShipCountry = 'UK'"));   
rstFiltered = rstOrders.OpenRecordset();

Note To change the contents of rstOrders, you must set its Filter property, and then open a second Recordset object based on rstOrders.

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; 
CdbRecordset      rstFiltered;

dbsNorthwind =  dbeng. OpenDatabase(_T("Northwind.mdb"));
rstOrders = dbsNorthwind.OpenRecordset(_T("SELECT * FROM Orders WHERE ShipCountry = 'UK';"));