Setting Up the Filter in DaoEnrol

Note   You’ve already added the code to filter and parameterize the CSectionSet recordset (in OnInitialUpdate); the code in this topic is for illustrative purposes only. Do not add the code from this topic to your source files.

Suggested Reading in the Visual C++ Programmer’s Guide

A recordset filter determines what subset of records are selected from a table or query. To add a filter, you simply set the value of CDaoRecordset::m_strFilter before calling CDaoRecordset::Open. For example, the following code selects just the class section records for course MATH101:

m_pSet->m_strFilter = "CourseID = 'MATH101'";
m_pSet->Open();

Because the base class CDaoRecordView::OnInitialUpdate calls CDaoRecordset::Open, all you need to do to initially select the records for MATH101, for example, is replace the following AppWizard implementation of OnInitialUpdate:

void CSectionForm::OnInitialUpdate()
{
   m_pSet = &GetDocument()->m_sectionSet;
   CDaoRecordView::OnInitialUpdate();
}

with:

void CSectionForm::OnInitialUpdate()
{
   m_pSet = &GetDocument()->m_sectionSet;
   m_pSet->m_strFilter = "CourseID = 'MATH101'";
   CDaoRecordView::OnInitialUpdate();
}

The filter can be any logical expression that is legal for the SQL WHERE clause. For example, the following is legal:

m_pSet->m_strFilter = 
            "CourseID = 'MATH101' AND InstructorID = 'ROGERSN'";

As an alternative, you can specify a complete SQL SELECT statement with a WHERE clause. In that case, you don’t use m_strFilter.

Examine the OnInitialUpdate code you added earlier in the procedure To fill the combo box. This code shows the filter for CSectionSet.