HOWTO: Connecting a CRecordset to a CDialog

Last reviewed: June 26, 1997
Article ID: Q125367
The information in this article applies to:
  • The Microsoft Foundation Classes, included with: - Microsoft Visual C++ for Windows, versions 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 4.0, 4.1,

         4.2, 4.2b, 5.0
    

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.

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:

  1. 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:

    a. 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.
    
    b. Start ClassWizard (CTRL+W). c. Select the 'CAboutDlg' class in the 'Class Name' list box. d. In ClassWizard, select the 'Class Info' tab. e. In the 'Foreign Class' list box, select 'CSectionSet'. CSectionSet is

          the CRecordset class that you need to associate with the dialog.
    
    f. In the 'Foreign Variable' edit control, type m_pSet (an arbitrary

          name).
    

    This creates a member variable m_pSet for the CAboutDlg class. The variable m_pSet will be a pointer to a CSectionSet object.

  2. 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.

  3. 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.
    
    

REFERENCES

To get more information about the 'Foreign Class' and 'Foreign Variable' features of ClassWizard, go to the 'Class Info' tab in ClassWizard and click Help.


Keywords : kbprg MfcDatabase kbprg
Technology : kbMfc
Version : 1.5 1.51 1.52 2.0 2.1 4.0 4.1
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.