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

  1. // DAOEMdoc.cpp : implementation of the CDAOEMPDoc class
  2. //
  3. // The document in this example holds the database connection
  4.  
  5.  
  6. #include "stdafx.h"
  7. #include "DAOEMP.h"
  8.  
  9. #include "DAOEMdoc.h"
  10. #include "DAOEMvw.h"
  11.  
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /*****************************************************************************
  18. * Forwards
  19. */
  20. class CDAOEMPView;
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CDAOEMPDoc
  24.  
  25. IMPLEMENT_DYNCREATE(CDAOEMPDoc, CDocument)
  26.  
  27. BEGIN_MESSAGE_MAP(CDAOEMPDoc, CDocument)
  28.     //{{AFX_MSG_MAP(CDAOEMPDoc)
  29.     ON_COMMAND(ID_EDIT_NEXT, OnEditNext)
  30.     ON_COMMAND(ID_EDIT_PREVIOUS, OnEditPrevious)
  31.     ON_COMMAND(ID_EDIT_ADD, OnEditAdd)
  32.     ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
  33.     ON_UPDATE_COMMAND_UI(ID_EDIT_DELETE, OnUpdateEditDelete)
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CDAOEMPDoc construction/destruction
  39.  
  40. CDAOEMPDoc::CDAOEMPDoc()
  41. {
  42.  
  43. }
  44.  
  45. CDAOEMPDoc::~CDAOEMPDoc()
  46. {
  47. }
  48.  
  49. BOOL CDAOEMPDoc::OnNewDocument()
  50. {
  51.     if (!COleDocument::OnNewDocument())
  52.         return FALSE;
  53.     
  54.     //Ask the user to point to the EMPLOYEE database
  55.     if(!ConnectToDatabase())
  56.         {
  57.         m_bConnected = FALSE;
  58.         return FALSE;
  59.         }
  60.  
  61.     m_bConnected = TRUE;
  62.     return TRUE;
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CDAOEMPDoc serialization
  67.  
  68. void CDAOEMPDoc::Serialize(CArchive& ar)
  69. {
  70.     if (ar.IsStoring())
  71.     {
  72.         // TODO: add storing code here
  73.     }
  74.     else
  75.     {
  76.         // TODO: add loading code here
  77.     }
  78. }
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CDAOEMPDoc diagnostics
  82.  
  83. #ifdef _DEBUG
  84. void CDAOEMPDoc::AssertValid() const
  85. {
  86.     CDocument::AssertValid();
  87. }
  88.  
  89. void CDAOEMPDoc::Dump(CDumpContext& dc) const
  90. {
  91.     CDocument::Dump(dc);
  92. }
  93. #endif //_DEBUG
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CDAOEMPDoc support 
  98.  
  99. //When the document is created, connect to the database and open the
  100. //Employee recordset.
  101. BOOL CDAOEMPDoc::ConnectToDatabase()
  102. {
  103.     CFileDialog        cOpenFile(    TRUE, 
  104.                                 _T("MDB"), 
  105.                                 _T("employee.mdb"), 
  106.                                 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
  107.                                 (_T("Access Files (*.mdb) | *.mdb ||")));
  108.  
  109.  
  110.  
  111.     //Get the location of database (assume it's the Employee example)
  112.     cOpenFile.DoModal();
  113.  
  114.     //Open the database and the recordset
  115.     try
  116.         {
  117.         //NOTE: Using default collection rather than Workspaces.Items
  118.         m_cEmpDatabase = m_cDBEngine.OpenDatabase(cOpenFile.m_ofn.lpstrFile);
  119.         m_cEmpRecordSet = m_cEmpDatabase.OpenRecordset(_T("Employees"));
  120.         }
  121.  
  122.     catch (CdbException e)
  123.         {
  124.         CdbLastOLEError exError;
  125.         TCHAR szBuf[256];
  126.  
  127.         wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(e.m_hr), (LPCTSTR) exError.GetDescription());
  128.         AfxMessageBox(szBuf);
  129.         return (FALSE);
  130.         }
  131.     
  132.     return TRUE;;
  133. }
  134.  
  135.  
  136. //Move to the next record
  137. void CDAOEMPDoc::OnEditNext() 
  138. {
  139.     if(!OKToMove()) 
  140.         return;
  141.  
  142.     m_cEmpRecordSet.MoveNext();
  143.  
  144.     //Watch for end of record set
  145.     if(m_cEmpRecordSet.GetEOF())
  146.         {
  147.         MessageBeep(0);
  148.         m_cEmpRecordSet.MovePrevious();
  149.         }
  150.     else
  151.         {
  152.         UpdateAllViews(NULL);
  153.         }
  154.     
  155. }
  156.  
  157. //Move to Previous record
  158. void CDAOEMPDoc::OnEditPrevious() 
  159. {
  160.     if(!OKToMove())
  161.         return;
  162.  
  163.     m_cEmpRecordSet.MovePrevious();
  164.  
  165.     //Watch for beginning of recordset
  166.     if(m_cEmpRecordSet.GetBOF())
  167.         {
  168.         MessageBeep(0);
  169.         m_cEmpRecordSet.MoveNext();
  170.         }
  171.     else
  172.         {
  173.         UpdateAllViews(NULL);
  174.         }
  175. }
  176.  
  177.  
  178. //Add a new record
  179. void CDAOEMPDoc::OnEditAdd() 
  180. {
  181.     if(!OKToMove())
  182.         return;
  183.  
  184.     //Remember where we were before adding in case the user
  185.     //cancels and we have to return
  186.     m_cLastGoodRecord = m_cEmpRecordSet.GetBookmark();
  187.     m_cEmpRecordSet.AddNew();
  188.     
  189.     UpdateAllViews(NULL);
  190. }
  191.  
  192.  
  193. //Is it OK to move to a different record(i.e. do we have "dirty" that need updating
  194. BOOL CDAOEMPDoc::OKToMove()
  195. {
  196.     POSITION pos = GetFirstViewPosition();
  197.     CDAOEMPView* pView = (CDAOEMPView*)GetNextView(pos);
  198.     
  199.     return pView->CommitAlteredEmpRec();
  200. }
  201.  
  202.  
  203. //Update 
  204. void CDAOEMPDoc::UpdateEmpRec(long m_nEmpNum, LPCTSTR lpszFirstName, 
  205.             LPCTSTR lpszHomePhone, LPCTSTR lpszLastName,    
  206.             LPCTSTR lpszNotes, DATE HireDate)
  207. {
  208.     //Convert the date to a dbVariant
  209.     COleVariant cdbHireDate;
  210.     cdbHireDate.date = HireDate;
  211.     cdbHireDate.vt = VT_DATE;
  212.  
  213.     try
  214.         {
  215.         //The recordset must be in edit mode
  216.         if(m_cEmpRecordSet.GetEditMode() == dbEditNone)
  217.             m_cEmpRecordSet.Edit();
  218.  
  219.         m_cEmpRecordSet.SetField(EMP_FIRST_NAME, COleVariant(lpszFirstName, VT_BSTRT));
  220.         m_cEmpRecordSet.SetField(EMP_HOME_PHONE, COleVariant(lpszHomePhone, VT_BSTRT));
  221.         m_cEmpRecordSet.SetField(EMP_LAST_NAME, COleVariant(lpszLastName, VT_BSTRT));
  222.         m_cEmpRecordSet.SetField(EMP_NOTES, COleVariant(lpszNotes, VT_BSTRT));
  223.         m_cEmpRecordSet.SetField(EMP_HIRE_DATE, cdbHireDate);
  224.  
  225.         //Commit the changes
  226.         m_cEmpRecordSet.Update();
  227.  
  228.         //Return to the edited record
  229.         CdbBookmark cBookmark = m_cEmpRecordSet.GetLastModified();
  230.         m_cEmpRecordSet.SetBookmark(cBookmark);
  231.         }
  232.  
  233.     catch (CdbException e)
  234.         {
  235.         CdbLastOLEError exError;
  236.         TCHAR szBuf[256];
  237.  
  238.         wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
  239.         AfxMessageBox(szBuf);
  240.         }
  241. }
  242.  
  243. //Delete a record
  244. void CDAOEMPDoc::OnEditDelete() 
  245. {
  246.     //Delete method depends on current mode
  247.     
  248.     short nEditMode = m_cEmpRecordSet.GetEditMode();
  249.  
  250.     try
  251.         {
  252.         switch (nEditMode)
  253.             {
  254.             case dbEditNone: // Just delete it
  255.                 {
  256.                 m_cEmpRecordSet.Delete();
  257.                 m_cEmpRecordSet.MoveFirst();
  258.                 break;
  259.                 }
  260.     
  261.             case dbEditInProgress: //Forget changes
  262.                 {
  263.                 m_cEmpRecordSet.CancelUpdate();
  264.                 m_cEmpRecordSet.Delete();
  265.                 m_cEmpRecordSet.MoveFirst();
  266.                 break;
  267.                 }
  268.  
  269.             case dbEditAdd: //If new record, go back to last known
  270.                 {
  271.                 m_cEmpRecordSet.CancelUpdate();
  272.                 m_cEmpRecordSet.SetBookmark(m_cLastGoodRecord);
  273.                 }
  274.             }
  275.  
  276.         UpdateAllViews(NULL);
  277.         }
  278.  
  279.     catch (CdbException e)
  280.         {
  281.         CdbLastOLEError exError;
  282.         TCHAR szBuf[256];
  283.  
  284.         wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
  285.         AfxMessageBox(szBuf);
  286.         }
  287. }
  288.  
  289.  
  290. void CDAOEMPDoc::OnUpdateEditDelete(CCmdUI* pCmdUI) 
  291. {
  292.     //Enable delete only if there are records 
  293.     if(m_cEmpRecordSet.GetRecordCount() < 1)
  294.         pCmdUI->Enable(FALSE);
  295. }
  296.