Click here to Skip to main content
12,348,726 members (29,116 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked

Making sure data is saved when closing a CRecordView application

, 26 Feb 2006
Rate this:
Please Sign up or sign in to vote.
An extension of the application in CH 14 of 'Teach Yourself Visual C++ 6 in 21 Days' by Davis Chapman.


See Chapter 14 of 'Teach Yourself Visual C++ 6 in 21 Days' for the background to this article.

In Chapter 13 of 'Teach Yourself Visual C++ 6 in 21 Days', you learn how there is a nice, complete, and very rounded way of making sure that data from a straightforward class is practically always saved. This is the 'Serialization' framework. However, when I built the example in Chapter 14, an application which uses a class inherited from the RecordView class, I found that I couldn't find a way to use the serialization framework. This meant that it was possible to edit a field, close the application, and data from that field would be lost. I posted my concerns to, and got some useful replies - and with a bit of Googling, came up with the solution described here. This is what I did:

  1. Created a function CView::OnChange() to handle the control's EN_CHANGE events.
  2. void CView::OnChange()
       // TODO: If this is a RICHEDIT control, the control will not
       // send this notification unless you override the           
       // CRecordView::OnInitDialog()
       // function and call CRichEditCtrl().SetEventMask()
       // with the ENM_CHANGE flag ORed into the mask.
       // TODO: Add your control notification handler code here
  3. At this point, I realized that I needed to call the UpDateData function in the view class from the CDoc::SaveModified() function. I couldn't work out how to access the view class, so I did some searches on Google, and found the following information from posted by kin...
  4. MFC (Access to CView from Anywhere)

    This is the cheats way of getting access to CView from anywhere in your application.

    1. In you CWinApp, create two functions: a void function StoreMyView (CView* pView), and a CView* function GetMyView().
    2. Declare a pointer of type CView in the CWinApp class.
    3. In the CView constructor, get the application using the following lines:
    4. CWinApp* pApp = (CWinApp*) AfxGetApp ();
      pApp->StoreMyView (this);
    5. In CWinApp GetMyView(), return your declared pointer.
    6. In CWinApp StoreMyView, point your data member to the parameter value. Say, the data member is m_pView and the parameter is pView; then m_pView = pView;.
    7. Now, you can access your view using the application object, i.e.:
    8. CWinApp * pApp = (CWinApp*) AfxGetApp ();
      CView * pView = pApp. GetMyView ();
    9. Don't forget to include View.h and Doc.h in CWinApp.h, and view.h is included in .cpp files automatically when you create your data member in VC++.

    That's all folks. Any comments, mail to:

    I used this method and it seems to do exactly what I wanted. I would love to know why the author says it is a cheat though.

  5. Using the above method, my CDoc::SaveModified() turned out as follows...
  6. BOOL CDoc::SaveModified()
       // TODO: Add your specialized code here and/or call the base class
       if (IsModified()!=0){
          if (AfxMessageBox("There is unsaved data. Do you want to save it?",
             // Save changes to the record
             if (m_dbOdbcSet.CanUpdate() && !m_dbOdbcSet.IsDeleted())
                //These three lines make use of the 'cheat' method
                //for making the view class accessible
                CApp * pApp = (CApp*) AfxGetApp ();
                CView * pView = pApp-> GetMyView ();
             return FALSE;
       //return CDocument::SaveModified();
       return TRUE;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 26 Feb 2006
Article Copyright 2006 by Ben Aldhouse
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid