Filling the Combo Box in DaoEnrol

A good place to fill the combo box with a list of course names is the CSectionForm override of CDaoRecordView’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. Using ClassView, expand the view of CSectionForm.

  2. Double-click OnInitialUpdate to jump to its definition in SectForm.cpp.

  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
    CDaoEnrolDoc* pDoc = GetDocument();
    pDoc->m_courseSet.m_strSort = "CourseID";
    
    try
    {
    pDoc->m_courseSet.Open();
    }
    catch(CDaoException* e)
    {
    AfxMessageBox(e->
            m_pErrorInfo->m_strDescription);
    e->Delete();
    return;
    }
    
    // Filter, parameterize and sort the 
    // CSectionSet recordset
    
    m_pSet->m_strFilter = 
        "CourseID = CourseIDParam";
    m_pSet->m_strCourseIDParam = 
        pDoc->m_courseSet.m_CourseID;
    m_pSet->m_strSort = "SectionNo";
    m_pSet->m_pDatabase = 
        pDoc->m_courseSet.m_pDatabase;
    

    Catch any exceptions so that errors are reported to the user. Unlike ODBC, DAO errors come directly from the underlying DAO object. Additional information about DAO errors can be found in the DAO SDK.

    You’ll add the necessary member declaration in the topic Parameterizing the Filter in DaoEnrol.

  4. Next, after the last line:
    CDaoRecordView::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.