Filling the Combo Box with a List of Courses

A good place to fill the combo box with a list of course names is in CSectionForm’s override of CRecordView’s OnInitialUpdate member function. As part of its own initialization, the form fills the combo box. The overall logic is as follows:

  1. Construct and open a CCourseSet recordset based on the Course table.

  2. Remove any current entries in the combo box.

  3. For each course name in CCourseSet, add the CourseID to the combo box.

  4. Set the selection to the first course name (as sorted) in the combo box.

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

The code in the following procedure fills the combo box and also filters, parameterizes, and sorts the CSectionSet recordset. Filtering, parameterization, and sorting are explained in topics that follow.

To fill the combo box

  1. From the Window menu, select the file SectionForm.cpp. (This file should still be open from the previous procedure.)

  2. From WizardBar, in the Members combo box, click OnInitialUpdate.

  3. Just after the first line — m_pSet = &GetDocument()->m_sectionSet; — add the code below (don’t replace any code):
    // Fill the combo box with all of the courses
    CEnrollDoc* pDoc = GetDocument();
    pDoc->m_courseSet.m_strSort = "CourseID";
    if (!pDoc->m_courseSet.Open())
    return;
    
    // Filter, parameterize and sort the CSectionSet recordset 
    m_pSet->m_strFilter = "CourseID = ?";
    m_pSet->m_strCourseIDParam = pDoc->m_courseSet.m_CourseID;
    m_pSet->m_strSort = "SectionNo";
    m_pSet->m_pDatabase = pDoc->m_courseSet.m_pDatabase; 
    

    You will add the necessary member declaration in Setting Up the Parameter later in this lesson.

  4. Next, after the last line (CRecordView::OnInitialUpdate();), add the following code:
    m_ctlCourseList.ResetContent();
    if (pDoc->m_courseSet.IsOpen())
    { 
    while (!pDoc->m_courseSet.IsEOF())
    {
    m_ctlCourseList.AddString(
    pDoc->m_courseSet.m_CourseID);
    pDoc->m_courseSet.MoveNext();
    }
    }
    m_ctlCourseList.SetCurSel(0);
    
  5. Save your work.

The code you have added above is explained in the topics Setting Up the Filter and Setting Up the Parameter, later in this lesson.