home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / daoenrol / addform.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  2.9 KB  |  148 lines

  1. // addform.cpp : implementation of the CAddForm class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "daoenrol.h"
  15. #include "addform.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. IMPLEMENT_DYNAMIC(CAddForm, CDaoRecordView)
  23.  
  24. BEGIN_MESSAGE_MAP(CAddForm, CDaoRecordView)
  25.     ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
  26.     ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  27.     ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
  28. END_MESSAGE_MAP()
  29.  
  30. CAddForm::CAddForm(UINT nIDTemplate)
  31.     : CDaoRecordView(nIDTemplate)
  32. {
  33.     m_bAddMode = FALSE;
  34. }
  35.  
  36. CAddForm::~CAddForm()
  37. {
  38. }
  39.  
  40. BOOL CAddForm::OnMove(UINT nIDMoveCommand)
  41. {
  42.     CDaoRecordset* pRecordset = OnGetRecordset();
  43.     if (m_bAddMode)
  44.     {
  45.         if (!UpdateData())
  46.             return FALSE;
  47.         try
  48.         {
  49.             pRecordset->Update();
  50.         }
  51.         catch (CDaoException* e)
  52.         {
  53.             AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  54.             e->Delete();
  55.             return FALSE;
  56.         }
  57.  
  58.         pRecordset->Requery();
  59.         UpdateData(FALSE);
  60.         m_bAddMode = FALSE;
  61.         return TRUE;
  62.     }
  63.     else
  64.     {
  65.         return CDaoRecordView::OnMove(nIDMoveCommand);
  66.     }
  67. }
  68.  
  69. BOOL CAddForm::RecordAdd()
  70. {
  71.     // If already in add mode, then complete previous new record
  72.     if (m_bAddMode)
  73.         OnMove(ID_RECORD_FIRST);
  74.     OnGetRecordset()->AddNew();
  75.     m_bAddMode = TRUE;
  76.     UpdateData(FALSE);
  77.     return TRUE;
  78. }
  79.  
  80. BOOL CAddForm::RecordDelete()
  81. {
  82.     CDaoRecordset* pRecordset = OnGetRecordset();
  83.     try
  84.     {
  85.         pRecordset->Delete();
  86.     }
  87.     catch (CDaoException* e)
  88.     {
  89.         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  90.         e->Delete();
  91.         return FALSE;
  92.     }
  93.  
  94.     // Move to the next record after the one just deleted
  95.         pRecordset->MoveNext();
  96.  
  97.     // If we moved off the end of file, then move back to last record
  98.     if (pRecordset->IsEOF())
  99.         pRecordset->MoveLast();
  100.  
  101.     // If the recordset is now empty, then clear the fields
  102.     // left over from the deleted record
  103.     if (pRecordset->IsBOF())
  104.         pRecordset->SetFieldNull(NULL);
  105.     UpdateData(FALSE);
  106.     return TRUE;
  107. }
  108.  
  109.  
  110. BOOL CAddForm::RecordRefresh()
  111. {
  112.  
  113.     if (m_bAddMode == TRUE)
  114.     {
  115.         OnGetRecordset()->Move(0);
  116.         m_bAddMode = FALSE;
  117.     }
  118.     // Copy fields from recordset to form, thus
  119.     // overwriting any changes user may have made
  120.     // on the form
  121.     UpdateData(FALSE);
  122.  
  123.     return TRUE;
  124. }
  125.  
  126. void CAddForm::OnRecordAdd()
  127. {
  128.     RecordAdd();
  129. }
  130.  
  131. void CAddForm::OnUpdateRecordFirst(CCmdUI* pCmdUI)
  132. {
  133.     if (m_bAddMode)
  134.         pCmdUI->Enable(TRUE);
  135.     else
  136.         CDaoRecordView::OnUpdateRecordFirst(pCmdUI);
  137. }
  138.  
  139. void CAddForm::OnRecordRefresh()
  140. {
  141.     RecordRefresh();
  142. }
  143.  
  144. void CAddForm::OnRecordDelete()
  145. {
  146.     RecordDelete();
  147. }
  148.