Microsoft KB Archive/125367

From BetaArchive Wiki
Knowledge Base


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
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 4.1 Subscription
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q125367

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:

    1. 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.
    2. Start ClassWizard (CTRL+W).
    3. Select the 'CAboutDlg' class in the 'Class Name' list box.
    4. In ClassWizard, select the 'Class Info' tab.
    5. In the 'Foreign Class' list box, select 'CSectionSet'. CSectionSet is the CRecordset class that you need to associate with the dialog.
    6. 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.


Additional query words: kbvc150 kbvc151 kbvc152 kbvc200 kbvc210 kbvc400 kbvc410 kbvc420 kbvc500 kbvc600

Keywords: kbhowto kbdatabase KB125367