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 / crsform.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.9 KB  |  178 lines

  1. // crsform.cpp : implementation file
  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.  
  16. #include "coursese.h"
  17. #include "addform.h"
  18. #include "crsform.h"
  19. #include "sectset.h"
  20. #include "denrldoc.h"
  21. #include "mainfrm.h"
  22. #include "resource.h"
  23.  
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CCourseForm
  32.  
  33. IMPLEMENT_DYNCREATE(CCourseForm, CAddForm)
  34.  
  35. BEGIN_MESSAGE_MAP(CCourseForm, CAddForm)
  36.     //{{AFX_MSG_MAP(CCourseForm)
  37.     ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  38.     ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
  39.     ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. CCourseForm::CCourseForm()
  44.     : CAddForm(CCourseForm::IDD)
  45. {
  46.     //{{AFX_DATA_INIT(CCourseForm)
  47.     m_pSet = NULL;
  48.     //}}AFX_DATA_INIT
  49. }
  50.  
  51. CCourseForm::~CCourseForm()
  52. {
  53. }
  54.  
  55. void CCourseForm::DoDataExchange(CDataExchange* pDX)
  56. {
  57.     CDaoRecordView::DoDataExchange(pDX);
  58.     //{{AFX_DATA_MAP(CCourseForm)
  59.     DDX_Control(pDX, IDC_COURSEID, m_ctlCourseID);
  60.     DDX_FieldText(pDX, IDC_COURSEID, m_pSet->m_CourseID, m_pSet);
  61.     DDX_FieldText(pDX, IDC_HOURS, m_pSet->m_Hours, m_pSet);
  62.     DDX_FieldText(pDX, IDC_TITLE, m_pSet->m_CourseTitle, m_pSet);
  63.     //}}AFX_DATA_MAP
  64. }
  65.  
  66. BOOL CCourseForm::OnMove(UINT nIDMoveCommand)
  67. {
  68.     BOOL bWasAddMode = FALSE;
  69.     CString strCourseID;
  70.     if (m_bAddMode == TRUE)
  71.     {
  72.         m_ctlCourseID.GetWindowText(strCourseID);
  73.         bWasAddMode = TRUE;
  74.     }
  75.     if (CAddForm::OnMove(nIDMoveCommand))
  76.     {
  77.         m_ctlCourseID.SetReadOnly(TRUE);
  78.         if (bWasAddMode == TRUE)
  79.         {
  80.             CUpdateHint hint;
  81.             hint.m_strCourse = strCourseID;
  82.             GetDocument()->UpdateAllViews(this, HINT_ADD_COURSE, &hint);
  83.         }
  84.         return TRUE;
  85.     }
  86.     return FALSE;
  87. }
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CCourseForm message handlers
  91.  
  92. CDaoRecordset* CCourseForm::OnGetRecordset()
  93. {
  94.     return m_pSet;
  95. }
  96.  
  97. void CCourseForm::OnInitialUpdate()
  98. {
  99.     CDaoEnrolDoc* pDoc = (CDaoEnrolDoc*)GetDocument();
  100.     CDaoDatabase* pDatabase = pDoc->GetDatabase();
  101.     if (!pDatabase->IsOpen())
  102.         return;
  103.     m_pSet = &pDoc->m_courseSetForForm;
  104.     m_pSet->m_strSort = "CourseID";
  105.     m_pSet->m_pDatabase = pDatabase;
  106.     CDaoRecordView::OnInitialUpdate();
  107. }
  108.  
  109.  
  110. void CCourseForm::OnRecordAdd()
  111. {
  112.     CAddForm::RecordAdd();
  113.     m_ctlCourseID.SetReadOnly(FALSE);
  114. }
  115.  
  116.  
  117. void CCourseForm::OnRecordDelete()
  118. {
  119.     // The STDREG.MDB Student Registration database in Access Format
  120.     // does not require a programmatic validation to
  121.     // assure that a course is not deleted if any sections exist.
  122.     // That is because the STDREG.MDB database has been pre-built with
  123.     // such an referential integrity rule.  If the user tries to
  124.     // delete a course that has a section, a CDBException will be
  125.     // thrown, and ENROLL will display the SQL error message
  126.     // informing the user that the course cannot be deleted.
  127.     //
  128.     // A Student Registration database initialized by the STDREG
  129.     // tool will not have any such built-in referential integrity
  130.     // rules.  For such databases, the following code assumes the
  131.     // burden of assuring that the course is not deleted if a section
  132.     // exists.  The reason that STDREG does not add referential
  133.     // integrity checks to the Student Registration database is that
  134.     // some databases such as SQL Server do not offer SQL, via ODBC,
  135.     // for creating referential integrity rules such as "FOREIGN KEY".
  136.     //
  137.     // The deletion of a course is not the only place ENROLL
  138.     // needs a programmatic referential integrity check.  Another example
  139.     // is a check that a duplicate course or seciton is not added.
  140.     // For simplicity, ENROLL does not make these other checks.
  141.  
  142.  
  143.     CDaoEnrolDoc* pDoc = (CDaoEnrolDoc*)GetDocument();
  144.     CsectionSet sectionSet;
  145.     sectionSet.m_pDatabase = pDoc->GetDatabase();
  146.     sectionSet.m_strFilter = "CourseID = CourseIDParam";
  147.     sectionSet.m_strCourseIDParam = m_pSet->m_CourseID;
  148.     try
  149.     {
  150.         sectionSet.Open();
  151.     }
  152.     catch (CDaoException* e)
  153.     {
  154.         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  155.         e->Delete();
  156.     }
  157.     if (!sectionSet.IsEOF())
  158.     {
  159.         AfxMessageBox(IDS_CANNOT_DELETE_COURSE_WITH_SECTION);
  160.         return;
  161.     }
  162.  
  163.     CUpdateHint hint;
  164.     hint.m_strCourse = m_pSet->m_CourseID;
  165.     if (CAddForm::RecordDelete())
  166.         GetDocument()->UpdateAllViews(this, HINT_DELETE_COURSE, &hint);
  167. }
  168.  
  169. void CCourseForm::OnRecordRefresh()
  170. {
  171.     if (m_bAddMode == TRUE)
  172.     {
  173.         m_pSet->CancelUpdate();
  174.         m_ctlCourseID.SetReadOnly(TRUE);
  175.     }
  176.     CAddForm::RecordRefresh();
  177. }
  178.