This topic explains how you might commonly customize the default
code for record views that the wizard writes for you.
A D V E R T I S E M E N T
want to constrain the record selection with a
sort the records, customize the SQL statement.
This information applies to both
CRecordView (ODBC) and
Using CRecordView or
CDaoRecordView is much the same as using
CFormView. The basic approach is to use the record view to
display and perhaps update the records of a single recordset. Beyond
that, you might want to use other recordsets as well, as discussed
Record Views: Filling a List Box from a Second Recordset.
Record View Code Created by Application Wizard
MFC Application Wizard overrides the view's OnInitialUpdate
and OnGetRecordset member functions. After the framework
creates the frame window, document, and view, it calls
OnInitialUpdate to initialize the view.
OnInitialUpdate obtains a pointer to the
recordset from the document. A call to the base class
CView::OnInitialUpdate function opens the recordset. The
following code shows this process for a CRecordView — the
code for a CDaoRecordView is similar:
When the recordset opens, it selects records.
CDaoRecordset::Open makes the first record the current record,
and DDX moves data from the recordset's field data members to the
corresponding form controls in the view. For more information about
Record Field Exchange (RFX). For more information about DDX, see
Dialog Data Exchange and Validation. For information about the
document/view creation process, see
Using the Classes to Write Applications for Windows.
You should give your end users the capability to
refresh the record view controls from the recordset.
Without this capability, if a user changes a control's
value to an illegal value, the user can be permanently
stuck on the current record. To refresh the controls,
you call the CWnd member function
UpdateData with a parameter of FALSE.
Changes You Might Make to the Default Code
MFC Application Wizard writes a recordset class for you that
selects all records in a single table. You will often want to modify
that behavior in one or more of the following ways:
Set a filter or a sort order for the recordset. Do this in
OnInitialUpdate after the recordset
object is constructed but before its Open member function
is called. For more information, see
Recordset: Filtering Records (ODBC) and
Recordset: Sorting Records (ODBC).
Parameterize the recordset. Specify the actual run-time
parameter value after the filter. For more information, see
Recordset: Parameterizing a Recordset (ODBC)
Pass a customized SQL string to the
Open member function. For a discussion of what you can
accomplish with this technique , see
SQL: Customizing Your Recordset's SQL Statement (ODBC).
Filling a List Box from a Second Recordset
By default, a record view is associated with a single
recordset object, whose fields are mapped to the record
view's controls. Sometimes you might 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 topic explains how
and when to do that.
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
The model for this topic 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 following procedure uses a combo box but
applies equally to a list box.
To fill a combo box or
list box from a second recordset
Create the recordset object (CRecordset
CDaoRecordset for DAO).
Obtain a pointer to the
CComboBox object for the combo box control.
Empty the combo box of any previous
Move through all records in the recordset,
CComboBox::AddString for each string from
the current record you want to add to the combo
Initialize the selection in the combo box.
// Fill the combo box with all of the courses
CENROLLDoc* pDoc = GetDocument();
while (!pDoc->m_courseSet.IsEOF() )
This function uses a second recordset,
contains a record for each course offered, and a
m_ctlCourseList, which is stored in the
record view class.
The function gets m_courseSet
from the document and opens it. Then it empties
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.