home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / tutorial / enroll / step4 / addform.cpp next >
C/C++ Source or Header  |  1998-03-26  |  3KB  |  139 lines

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