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

  1. // sectform.cpp : implementation of the CSectionForm 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.  
  16. #include "sectset.h"
  17. #include "coursese.h"
  18. #include "denrldoc.h"
  19. #include "sectform.h"
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CSectionForm
  29.  
  30. IMPLEMENT_DYNCREATE(CSectionForm, CDaoRecordView)
  31.  
  32. BEGIN_MESSAGE_MAP(CSectionForm, CDaoRecordView)
  33.     //{{AFX_MSG_MAP(CSectionForm)
  34.     ON_CBN_SELENDOK(IDC_COURSELIST, OnSelendokCourselist)
  35.     ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  36.     ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
  37.     ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
  38.     //}}AFX_MSG_MAP
  39.     // Standard printing commands
  40.     ON_COMMAND(ID_FILE_PRINT, CDaoRecordView::OnFilePrint)
  41.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CDaoRecordView::OnFilePrint)
  42.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDaoRecordView::OnFilePrintPreview)
  43. END_MESSAGE_MAP()
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CSectionForm construction/destruction
  47.  
  48. CSectionForm::CSectionForm()
  49.     : CDaoRecordView(CSectionForm::IDD)
  50. {
  51.     //{{AFX_DATA_INIT(CSectionForm)
  52.     m_pSet = NULL;
  53.     //}}AFX_DATA_INIT
  54.     // TODO: add construction code here
  55.     m_bAddMode = FALSE;
  56. }
  57.  
  58. CSectionForm::~CSectionForm()
  59. {
  60. }
  61.  
  62. void CSectionForm::DoDataExchange(CDataExchange* pDX)
  63. {
  64.     CDaoRecordView::DoDataExchange(pDX);
  65.     //{{AFX_DATA_MAP(CSectionForm)
  66.     DDX_Control(pDX, IDC_SECTION, m_ctlSection);
  67.     DDX_Control(pDX, IDC_COURSELIST, m_ctlCourseList);
  68.     DDX_FieldText(pDX, IDC_INSTRUCTOR, m_pSet->m_InstructorID, m_pSet);
  69.     DDX_FieldText(pDX, IDC_ROOM, m_pSet->m_RoomNo, m_pSet);
  70.     DDX_FieldText(pDX, IDC_SCHEDULE, m_pSet->m_Schedule, m_pSet);
  71.     DDX_FieldText(pDX, IDC_SECTION, m_pSet->m_SectionNo, m_pSet);
  72.     DDX_FieldText(pDX, IDC_CAPACITY, m_pSet->m_Capacity, m_pSet);
  73.     DDX_FieldCBString(pDX, IDC_COURSELIST, m_pSet->m_CourseID, m_pSet);
  74.     //}}AFX_DATA_MAP
  75. }
  76.  
  77. BOOL CSectionForm::PreCreateWindow(CREATESTRUCT& cs)
  78. {
  79.     // TODO: Modify the Window class or styles here by modifying
  80.     //  the CREATESTRUCT cs
  81.  
  82.     return CDaoRecordView::PreCreateWindow(cs);
  83. }
  84.  
  85. void CSectionForm::OnInitialUpdate()
  86. {
  87.     CDaoEnrolDoc* pDoc = GetDocument();
  88.     m_pSet = &pDoc->m_sectionSet;
  89.     m_pSet->m_pDatabase = pDoc->GetDatabase();
  90.     if (!m_pSet->m_pDatabase->IsOpen())
  91.         return;
  92.  
  93.     // Fill the combo box with all of the courses
  94.  
  95.     pDoc->m_courseSet.m_strSort = "CourseID";
  96.     if (pDoc->m_courseSet.m_pDatabase == NULL)
  97.         pDoc->m_courseSet.m_pDatabase = pDoc->GetDatabase();
  98.     try
  99.     {
  100.         pDoc->m_courseSet.Open();
  101.     }
  102.     catch(CDaoException* e)
  103.     {
  104.         AfxMessageBox(e->
  105.             m_pErrorInfo->m_strDescription);
  106.         e->Delete();
  107.         return;
  108.     }
  109.  
  110.     // Filter, parameterize and sort the
  111.     // CSectionSet recordset
  112.  
  113.     m_pSet->m_strFilter =
  114.         "CourseID = CourseIDParam";
  115.     m_pSet->m_strCourseIDParam =
  116.         pDoc->m_courseSet.m_CourseID;
  117.     m_pSet->m_strSort = "SectionNo";
  118.     m_pSet->m_pDatabase =
  119.         pDoc->m_courseSet.m_pDatabase;
  120.  
  121.     CDaoRecordView::OnInitialUpdate();
  122.  
  123.     m_ctlCourseList.ResetContent();
  124.     if (pDoc->m_courseSet.IsOpen())
  125.     {
  126.         while (!pDoc->m_courseSet.IsEOF())
  127.         {
  128.             m_ctlCourseList.AddString(
  129.                 pDoc->m_courseSet.m_CourseID);
  130.             pDoc->m_courseSet.MoveNext();
  131.         }
  132.     }
  133.     m_ctlCourseList.SetCurSel(0);
  134.  
  135. }
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CSectionForm printing
  139.  
  140. BOOL CSectionForm::OnPreparePrinting(CPrintInfo* pInfo)
  141. {
  142.     // default preparation
  143.     return DoPreparePrinting(pInfo);
  144. }
  145.  
  146. void CSectionForm::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  147. {
  148.     // TODO: add extra initialization before printing
  149. }
  150.  
  151. void CSectionForm::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  152. {
  153.     // TODO: add cleanup after printing
  154. }
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157. // CSectionForm diagnostics
  158.  
  159. #ifdef _DEBUG
  160. void CSectionForm::AssertValid() const
  161. {
  162.     CDaoRecordView::AssertValid();
  163. }
  164.  
  165. void CSectionForm::Dump(CDumpContext& dc) const
  166. {
  167.     CDaoRecordView::Dump(dc);
  168. }
  169.  
  170. CDaoEnrolDoc* CSectionForm::GetDocument() // non-debug version is inline
  171. {
  172.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDaoEnrolDoc)));
  173.     return (CDaoEnrolDoc*)m_pDocument;
  174. }
  175. #endif //_DEBUG
  176.  
  177. /////////////////////////////////////////////////////////////////////////////
  178. // CSectionForm database support
  179. CDaoRecordset* CSectionForm::OnGetRecordset()
  180. {
  181.     return m_pSet;
  182. }
  183.  
  184.  
  185. /////////////////////////////////////////////////////////////////////////////
  186. // CSectionForm message handlers
  187.  
  188. void CSectionForm::OnSelendokCourselist()
  189. {
  190.     // TODO: Add your control notification handler code here
  191.     if (!m_pSet->IsOpen() )
  192.         return;
  193.     m_ctlCourseList.GetLBText(m_ctlCourseList.GetCurSel(),
  194.         m_pSet->m_strCourseIDParam);
  195.     if (!m_bAddMode)
  196.     {
  197.         m_pSet->Requery();
  198.         if (m_pSet->IsEOF())
  199.         {
  200.             m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
  201.             m_pSet->m_CourseID = m_pSet->m_strCourseIDParam;
  202.         }
  203.         UpdateData(FALSE);
  204.     }
  205.  
  206. }
  207.  
  208. void CSectionForm::OnRecordAdd()
  209. {
  210.     // If already in add mode, complete the previous new record
  211.     if (m_bAddMode)
  212.         OnMove(ID_RECORD_FIRST);
  213.  
  214.     CString strCurrentCourse = m_pSet->m_CourseID;
  215.     m_pSet->AddNew();
  216.     m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
  217.     m_pSet->m_CourseID = strCurrentCourse;
  218.     m_bAddMode = TRUE;
  219.     m_ctlSection.SetReadOnly(FALSE);
  220.     UpdateData(FALSE);
  221.  
  222. }
  223.  
  224. void CSectionForm::OnRecordDelete()
  225. {
  226.     try
  227.     {
  228.         m_pSet->Delete();
  229.     }
  230.     catch(CDaoException* e)
  231.     {
  232.         AfxMessageBox(e->
  233.             m_pErrorInfo->m_strDescription);
  234.         e->Delete();
  235.     }
  236.  
  237.     // Move to the next record after the one just deleted
  238.     m_pSet->MoveNext();
  239.  
  240.     // If we moved off the end of file, move back to last record
  241.     if (m_pSet->IsEOF())
  242.         m_pSet->MoveLast();
  243.  
  244.     // If the recordset is now empty, clear the fields left over
  245.     // from the deleted record
  246.     if (m_pSet->IsBOF())
  247.         m_pSet->SetFieldNull(NULL);
  248.     UpdateData(FALSE);
  249.  
  250. }
  251.  
  252. void CSectionForm::OnRecordRefresh()
  253. {
  254.     if (m_bAddMode)
  255.     {
  256.         m_pSet->CancelUpdate();
  257.         m_pSet->Move(0);
  258.         m_ctlSection.SetReadOnly(TRUE);
  259.         m_bAddMode = FALSE;
  260.     }
  261.     // Copy fields from recordset to form, thus
  262.     // overwriting any changes the user may have made
  263.     // on the form
  264.     UpdateData(FALSE);
  265.  
  266. }
  267.  
  268. BOOL CSectionForm::OnMove(UINT nIDMoveCommand)
  269. {
  270.     if (m_bAddMode)
  271.     {
  272.         if (!UpdateData())
  273.             return FALSE;
  274.         try
  275.         {
  276.             m_pSet->Update();
  277.         }
  278.         catch(CDaoException* e)
  279.         {
  280.             AfxMessageBox(e->
  281.                 m_pErrorInfo->m_strDescription);
  282.             e->Delete();
  283.         }
  284.         m_bAddMode = FALSE;
  285.         m_ctlSection.SetReadOnly(TRUE);
  286.         m_pSet->Requery();
  287.         UpdateData();
  288.     }
  289.     CDaoRecordView::OnMove(nIDMoveCommand);
  290.     return TRUE;
  291. //  else
  292. //  {
  293. //      AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  294. //  }
  295. }
  296.