home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_DAO.SDK / DISK4 / DAOSDK.1 / daoemvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  5.5 KB  |  224 lines

  1. // DAOEMvw.cpp : implementation of the CDAOEMPView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "DAOEMP.h"
  6.  
  7. #include "DAOEMdoc.h"
  8. #include "DAOEMvw.h"
  9.  
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char BASED_CODE THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CDAOEMPView
  18.  
  19. IMPLEMENT_DYNCREATE(CDAOEMPView, CFormView)
  20.  
  21. BEGIN_MESSAGE_MAP(CDAOEMPView, CFormView)
  22.     //{{AFX_MSG_MAP(CDAOEMPView)
  23.     //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CDAOEMPView construction/destruction
  28.  
  29. CDAOEMPView::CDAOEMPView():CFormView(IDD_MAINFORM)
  30. {
  31.     //{{AFX_DATA_INIT(CDAOEMPView)
  32.     m_nEmpNum = 0;
  33.     m_strFirstName = _T("");
  34.     m_strHomePhone = _T("");
  35.     m_strLastName = _T("");
  36.     m_strNotes = _T("");
  37.     //}}AFX_DATA_INIT
  38. }
  39.  
  40. CDAOEMPView::~CDAOEMPView()
  41. {
  42. }
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CDAOEMPView drawing
  46.  
  47. void CDAOEMPView::OnDraw(CDC* pDC)
  48. {
  49.     CDAOEMPDoc* pDoc = GetDocument();
  50.     ASSERT_VALID(pDoc);
  51.  
  52. }
  53.  
  54.  
  55. void CDAOEMPView::DoDataExchange(CDataExchange* pDX)
  56. {
  57.     // ClassWizard maintains mappings between data of the CDAOEMPView
  58.     // class and the controls in the dialog template resource for this
  59.     // form view.
  60.  
  61.     //{{AFX_DATA_MAP(CDAOEMPView)
  62.     DDX_Text(pDX, IDC_EMPNUM, m_nEmpNum);
  63.     DDV_MinMaxLong(pDX, m_nEmpNum, 1, 999);
  64.     DDX_Text(pDX, IDC_FIRSTNAME, m_strFirstName);
  65.     DDV_MaxChars(pDX, m_strFirstName, 10);
  66.     DDX_Text(pDX, IDC_HOMEPHONE, m_strHomePhone);
  67.     DDV_MaxChars(pDX, m_strHomePhone, 24);
  68.     DDX_Text(pDX, IDC_LASTNAME, m_strLastName);
  69.     DDV_MaxChars(pDX, m_strLastName, 20);
  70.     DDX_Text(pDX, IDC_NOTES, m_strNotes);
  71.     DDX_Text(pDX, IDC_HIREDATE, m_HireDate); 
  72.     //}}AFX_DATA_MAP
  73.  
  74. }
  75.  
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CDAOEMPView diagnostics
  79.  
  80. #ifdef _DEBUG
  81. void CDAOEMPView::AssertValid() const
  82. {
  83.     CFormView::AssertValid();
  84. }
  85.  
  86. void CDAOEMPView::Dump(CDumpContext& dc) const
  87. {
  88.     CFormView::Dump(dc);
  89. }
  90.  
  91. CDAOEMPDoc* CDAOEMPView::GetDocument() // non-debug version is inline
  92. {
  93.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDAOEMPDoc)));
  94.     return (CDAOEMPDoc*)m_pDocument;
  95. }
  96. #endif //_DEBUG
  97.  
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CDAOEMPView miscellaneous
  101.  
  102. void CDAOEMPView::SetFieldStates() 
  103. {
  104.     BOOL bEnabled = TRUE;
  105.     CWnd* pControl;
  106.     CDAOEMPDoc* pDoc = GetDocument();
  107.  
  108.     //If there's no currently connected database, then disable the form
  109.     if(pDoc == NULL || !pDoc->m_bConnected)
  110.         {
  111.         bEnabled = FALSE;
  112.         }
  113.  
  114.     pControl = GetTopWindow() ;
  115.  
  116.     //Disable all controls
  117.     while(pControl != NULL)
  118.         {
  119.         pControl->EnableWindow(bEnabled);
  120.         pControl = pControl->GetNextWindow();
  121.         }
  122.  
  123.     //Never enable the employee ID
  124.     (GetDlgItem(IDC_EMPNUM))->EnableWindow(FALSE);
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CDAOEMPView message handlers
  130.  
  131. void CDAOEMPView::OnInitialUpdate() 
  132. {
  133.     //Disable the fields if no databased attached
  134.     SetFieldStates();
  135.  
  136.     m_pSelection = NULL;    // initialize selection
  137.     
  138.     CFormView::OnInitialUpdate();
  139. }
  140.  
  141. void CDAOEMPView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  142. {
  143.     CDAOEMPDoc* pDoc = GetDocument();
  144.     ASSERT_VALID(pDoc);
  145.     try
  146.         {
  147.         m_nEmpNum        = VTOLONG(pDoc->m_cEmpRecordSet.Fields[EMP_EMPLOYEE_ID].GetValue());
  148.         
  149.         //Convert the variant strings into CStrings
  150.         VarToCStr(&m_strFirstName,    &pDoc->m_cEmpRecordSet.Fields[EMP_FIRST_NAME].GetValue());
  151.         VarToCStr(&m_strHomePhone,    &pDoc->m_cEmpRecordSet.Fields[EMP_HOME_PHONE].GetValue());
  152.         VarToCStr(&m_strLastName,    &pDoc->m_cEmpRecordSet.Fields[EMP_LAST_NAME].GetValue());
  153.         VarToCStr(&m_strNotes,        &pDoc->m_cEmpRecordSet.Fields[EMP_NOTES].GetValue());
  154.  
  155.         //COleDateTime 
  156.         m_HireDate        = pDoc->m_cEmpRecordSet.Fields[EMP_HIRE_DATE].GetValue();
  157.         }
  158.     catch (CdbException e)
  159.         {
  160.         CdbLastOLEError exError;
  161.         TCHAR szBuf[256];
  162.  
  163.         wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(e.m_hr), (LPCTSTR) exError.GetDescription());
  164.         AfxMessageBox(szBuf);
  165.         }
  166.  
  167.     UpdateData(FALSE); //Invoke Data Exchange (copy member data to form controls)
  168.  
  169.     Invalidate(); //Repaint
  170. }
  171.  
  172. BOOL CDAOEMPView::CommitAlteredEmpRec()
  173. {
  174.     CDAOEMPDoc* pDoc = GetDocument();
  175.     CString        strOldFirstName, strOldHomePhone, strOldLastName, strOldNotes;
  176.  
  177.     //Get current record string values
  178.     VarToCStr(&strOldFirstName,    &pDoc->m_cEmpRecordSet.GetField(EMP_FIRST_NAME));
  179.     VarToCStr(&strOldHomePhone,    &pDoc->m_cEmpRecordSet.GetField(EMP_HOME_PHONE));
  180.     VarToCStr(&strOldLastName,    &pDoc->m_cEmpRecordSet.GetField(EMP_LAST_NAME));
  181.     VarToCStr(&strOldNotes,        &pDoc->m_cEmpRecordSet.GetField(EMP_NOTES));
  182.  
  183.     //Force DDX to update member values
  184.     if(!UpdateData())
  185.         return FALSE;
  186.  
  187.     // Did any fields change value?
  188.     if(
  189.     (m_nEmpNum        == VTOLONG(pDoc->m_cEmpRecordSet.GetField(EMP_EMPLOYEE_ID)) ) &&
  190.     (m_strFirstName    == strOldFirstName    ) &&
  191.     (m_strHomePhone    == strOldHomePhone    ) &&
  192.     (m_strLastName    == strOldLastName    ) &&
  193.     (m_strNotes        == strOldNotes        ) &&
  194.     (m_HireDate        == pDoc->m_cEmpRecordSet.GetField(EMP_HIRE_DATE)))
  195.         return TRUE;
  196.  
  197.     //Save it, dump it or stay on it?
  198.     switch (AfxMessageBox(IDS_PROMPT_COMMIT_EMPREC, MB_YESNOCANCEL))
  199.         {
  200.         case IDYES:
  201.             {
  202.             pDoc->UpdateEmpRec(    m_nEmpNum,
  203.                                 m_strFirstName,
  204.                                 m_strHomePhone,    
  205.                                 m_strLastName,    
  206.                                 m_strNotes,        
  207.                                 (DATE)m_HireDate.m_dt);
  208.             return TRUE;
  209.             }
  210.  
  211.         case IDNO:
  212.             {
  213.             return TRUE;
  214.             }
  215.  
  216.         default:
  217.             {
  218.             GetDlgItem(IDC_EMPNUM)->SetFocus();
  219.             return TRUE;
  220.             }
  221.         }
  222.     }
  223.  
  224.