Share via


Record Views: Filling a List Box from a Second Recordset

OverviewHow Do IFAQSampleODBC Driver List

By default, a record view is associated with a single recordset object, whose fields are mapped to the record view’s controls. Sometimes you will want to put a list box or combo box control in your record view and fill it with values from a second recordset object. The user can use the list box to select a new category of information to display in the record view. This article explains how and when to do that.

Tip   Be aware that filling a combo box or list box from a data source might be slow. Take precautions against trying to fill a control from a recordset with a large number of records.

For example, the ENROLL tutorial in Tutorials uses a , CSectionForm, to display information about sections of college courses. (For DAO tutorial purposes, follow the ODBC ENROLL tutorial except for Step 1.) ENROLL binds the form’s controls to the field data members of a recordset of class CSectionSet. CSectionForm’s combo box control is filled from a second recordset of class CCourseSet that contains a record for each course offered at the school. When the user selects a new course in the combo box, the record view requeries the CSectionSet recordset to get the sections for the selected course. See the MFC Tutorial sample . Step 2 adds the combo box.

The model for this article, then, consists of a primary recordset that fills the controls of your form, while a secondary recordset fills a list box or combo box. Selecting a string from the list box causes your program to requery the primary recordset based on what was selected. The procedure below uses a combo box but applies equally to a list box.

To fill a combo box or list box from a second recordset

  1. Create the recordset object ( for ODBC, for DAO).

  2. Obtain a pointer to the object for the combo box control.

  3. Empty the combo box of any previous contents.

  4. Move through all records in the recordset, calling for each string from the current record you want to add to the combo box.

  5. Initialize the selection in the combo box.

The code in the OnInitialUpdate member function of class CSectionForm in the MFC Tutorial sample illustrates the procedure. The following excerpt shows how the combo box is filled by extracting a course ID value from each record in the recordset pointed to by pCourses. (The code for DAO is quite similar.)

void CSectionForm::OnInitialUpdate()
{
    // ...

    // Fill the combo box with all of the courses
    CENROLLDoc* pDoc = GetDocument();
    if (!pDoc->m_courseSet.Open())
        return;

    // ...

    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);
}

This function uses a second recordset, m_courseSet, which contains a record for each course offered, and a CComboBox control, m_ctlCourseList, which is stored in the record view class.

The function gets m_courseSet from the document and opens it. Then it empties m_ctlCourseList and scrolls through m_courseSet. For each record, the function calls the combo box’s AddString member function to add the course ID value from the record. Finally, the code sets the combo box’s selection.