Setting Up the Filter

Note   You have 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.

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 CRecordset::m_strFilter before calling CRecordset::Open. For example, the following code selects just the class section records for course MATH101:

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

Since the base class CRecordView::OnInitialUpdate calls CRecordset::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;
   CRecordView::OnInitialUpdate();
}

with:

void CSectionForm::OnInitialUpdate()
{
   m_pSet = &GetDocument()->m_sectionSet;
   m_pSet->m_strFilter = "CourseID = 'MATH101'";
   CRecordView::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'";

Examine the OnInitialUpdate code you added earlier. It shows the filter for CSectionSet in Enroll Step 2.

Caution   In Enroll, filter strings typically use a parameter placeholder, “?”, rather than assigning a specific literal value, such as “MATH101”, at compile time. If you do use literal strings in your filters (or other parts of the SQL statement), you may have to “quote” such strings with a DBMS-specific “literal prefix” and “literal suffix” character(s). For example, the code in this topic uses a single quote character to bracket the value assigned as the filter, “MATH101”. You may also encounter special syntactic requirements for operations such as outer joins, depending on your DBMS. Use ODBC functions to obtain this information from your driver for the DBMS. For example, call ::SQLGetTypeInfo for a particular data type, such as SQL_VARCHAR, to request the LITERAL_PREFIX and LITERAL_SUFFIX characters.