Microsoft KB Archive/125367

= How To Connecting a CRecordset to a CDialog =

Article ID: 125367

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 4.2 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q125367

<div class="summary_section">

SUMMARY
The Visual C++ ENROLL tutorial documentation describes how to connect controls of a CRecordView to the field variables of a CRecordset. With a little more work, you can also connect field variables to controls of a CDialog. This article explains the steps involved.

<div class="moreinformation_section">

MORE INFORMATION
You can connect a CRecordset to a CDialog in much the same way you connect a CRecordset to a CRecordView. Once you have your CRecordset and CDialog created, follow these steps:

<ol> <li>Using the ClassWizard, specify the CRecordset class as a 'foreign class' for the CDialog. Here's an example of how to do this with the CAboutDlg dialog in the ENROLL sample:

<ol> <li>Load the ENROLL step 1 project from the \MSVC\MFC\SAMPLES\ENROLL\STEP1 directory for Visual C++ 2.x, and from \MSDEV\SAMPLES\MFC\TUTORIAL\ENROLL\STEP1 for Visual C++ 4.0.</li> <li>Start ClassWizard (CTRL+W).</li> <li>Select the 'CAboutDlg' class in the 'Class Name' list box.</li> <li>In ClassWizard, select the 'Class Info' tab.</li> <li>In the 'Foreign Class' list box, select 'CSectionSet'. CSectionSet is the CRecordset class that you need to associate with the dialog.</li> <li>In the 'Foreign Variable' edit control, type m_pSet (an arbitrary name).</li></ol>

This creates a member variable m_pSet for the CAboutDlg class. The variable m_pSet will be a pointer to a CSectionSet object.</li> <li>In Visual C++ 4.0, use the Resource View, within the Project Workspace, to add controls to the CDialog. In earlier versions of Visual C++, use AppStudio to add the controls.</li> <li> Add code to the CDialog to assign, open, and close the recordset. For example: BOOL CAboutDlg::OnInitDialog {          CEnrollDoc * pDoc=(CEnrollDoc *) (GetParentFrame->                    GetActiveDocument); m_pSet= &(pDoc->m_SomeRecordset); m_pSet->Open;

CDialog::OnInitDialog;

return TRUE; } Before using this code, make sure the CSectionSet object (derived from CRecordset) is defined as the public member variable of the CEnrollDoc class (derived from CDocument). Note, in the code above, this member variable is named 'm_SomeRecordset You can add additional code to check for CDBExceptions and to allow the user to move through the records.

NOTE: this code does not close the CRecordset object. This could be done in OnOK and OnCancel in your CDialog's destructor, or you could just let it happen when the CRecordset object is itself destroyed. </li></ol>

<div class="references_section">