home *** CD-ROM | disk | FTP | other *** search
/ Looney Tunes Photo Fun / LooneyTunesPhotoFun.iso / Daosdk / DISK4 / DAOSDK.1 / daoemdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-06  |  6.8 KB  |  324 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.     //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CDAOEMPDoc construction/destruction
  38.  
  39. CDAOEMPDoc::CDAOEMPDoc()
  40. {
  41.  
  42. }
  43.  
  44. CDAOEMPDoc::~CDAOEMPDoc()
  45. {
  46. }
  47.  
  48. BOOL CDAOEMPDoc::OnNewDocument()
  49. {
  50.     if (!COleDocument::OnNewDocument())
  51.         return FALSE;
  52.  
  53.     //Assume it's not empty
  54.     m_bEmptyTable = FALSE;
  55.  
  56.     //Ask the user to point to the EMPLOYEE database
  57.     if(!ConnectToDatabase())
  58.         {
  59.         m_bConnected = FALSE;
  60.         return FALSE;
  61.         }
  62.  
  63.     m_bConnected = TRUE;
  64.     return TRUE;
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CDAOEMPDoc serialization
  69.  
  70. void CDAOEMPDoc::Serialize(CArchive& ar)
  71. {
  72.     if (ar.IsStoring())
  73.     {
  74.         // TODO: add storing code here
  75.     }
  76.     else
  77.     {
  78.         // TODO: add loading code here
  79.     }
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CDAOEMPDoc diagnostics
  84.  
  85. #ifdef _DEBUG
  86. void CDAOEMPDoc::AssertValid() const
  87. {
  88.     CDocument::AssertValid();
  89. }
  90.  
  91. void CDAOEMPDoc::Dump(CDumpContext& dc) const
  92. {
  93.     CDocument::Dump(dc);
  94. }
  95. #endif //_DEBUG
  96.  
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CDAOEMPDoc support 
  100.  
  101. //When the document is created, connect to the database and open the
  102. //Employee recordset.
  103. BOOL CDAOEMPDoc::ConnectToDatabase()
  104. {
  105.     CFileDialog        cOpenFile(    TRUE, 
  106.                                 _T("MDB"), 
  107.                                 _T("employee.mdb"), 
  108.                                 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
  109.                                 (_T("Access Files (*.mdb) | *.mdb ||")));
  110.  
  111.  
  112.  
  113.     //Get the location of database (assume it's the Employee example)
  114.     cOpenFile.DoModal();
  115.  
  116.     //Open the database and the recordset
  117.     try
  118.         {
  119.         //NOTE: Using default collection rather than Workspaces.Items
  120.         m_cEmpDatabase = m_cDBEngine.OpenDatabase(cOpenFile.m_ofn.lpstrFile);
  121.         m_cEmpRecordSet = m_cEmpDatabase.OpenRecordset(_T("Employees"));
  122.         m_cEmpRecordSet.MoveFirst();
  123.         }
  124.  
  125.     catch (CdbException dbExcept)
  126.         {
  127.         CdbLastOLEError exError;
  128.         TCHAR szBuf[256];
  129.  
  130.         //If error is "No Current Record" assume it's an empty table
  131.         if(dbExcept.m_hr == E_DAO_NoCurrentRecord)
  132.             {
  133.             m_bEmptyTable = TRUE;
  134.             }
  135.         else
  136.             {
  137.             wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbExcept.m_hr), (LPCTSTR) exError.GetDescription());
  138.             AfxMessageBox(szBuf);
  139.             return (FALSE);
  140.             }
  141.         }
  142.  
  143.  
  144.     return TRUE;
  145. }
  146.  
  147.  
  148. //Move to the next record
  149. void CDAOEMPDoc::OnEditNext() 
  150. {
  151.     if(!OKToMove()) 
  152.         return;
  153.  
  154.     m_cEmpRecordSet.MoveNext();
  155.  
  156.     //Watch for end of record set
  157.     if(m_cEmpRecordSet.GetEOF())
  158.         {
  159.         MessageBeep(0);
  160.         m_cEmpRecordSet.MovePrevious();
  161.         }
  162.     else
  163.         {
  164.         UpdateAllViews(NULL);
  165.         }
  166.     
  167. }
  168.  
  169. //Move to Previous record
  170. void CDAOEMPDoc::OnEditPrevious() 
  171. {
  172.     if(!OKToMove())
  173.         return;
  174.  
  175.     m_cEmpRecordSet.MovePrevious();
  176.  
  177.     //Watch for beginning of recordset
  178.     if(m_cEmpRecordSet.GetBOF())
  179.         {
  180.         MessageBeep(0);
  181.         m_cEmpRecordSet.MoveNext();
  182.         }
  183.     else
  184.         {
  185.         UpdateAllViews(NULL);
  186.         }
  187. }
  188.  
  189.  
  190. //Add a new record
  191. void CDAOEMPDoc::OnEditAdd() 
  192. {
  193.     //If the table is empty, add a blank record
  194.     if(m_bEmptyTable)
  195.         {
  196.         m_cEmpRecordSet.AddNew();
  197.         m_cEmpRecordSet.Update();
  198.         m_cEmpRecordSet.MoveFirst();
  199.         m_bEmptyTable = FALSE;
  200.         return;
  201.         }
  202.     else
  203.         {
  204.         if(!OKToMove())
  205.             return;
  206.  
  207.         //Remember where we were before adding in case the user
  208.         //cancels and we have to return
  209.         m_cLastGoodRecord = m_cEmpRecordSet.GetBookmark();
  210.         m_cEmpRecordSet.AddNew();
  211.         m_cEmpRecordSet.Fields[EMP_HIRE_DATE].SetValue(COleVariant(32874L)); // Default to 1/1/90
  212.         }
  213.  
  214.     
  215.     UpdateAllViews(NULL);
  216. }
  217.  
  218.  
  219. //Is it OK to move to a different record(i.e. do we have "dirty" that need updating
  220. BOOL CDAOEMPDoc::OKToMove()
  221. {
  222.     POSITION pos = GetFirstViewPosition();
  223.     CDAOEMPView* pView = (CDAOEMPView*)GetNextView(pos);
  224.  
  225.     if(m_bEmptyTable)
  226.         return FALSE;
  227.     
  228.     return pView->CommitAlteredEmpRec();
  229. }
  230.  
  231.  
  232. //Update 
  233. void CDAOEMPDoc::UpdateEmpRec(long m_nEmpNum, LPCTSTR lpszFirstName, 
  234.             LPCTSTR lpszHomePhone, LPCTSTR lpszLastName,    
  235.             LPCTSTR lpszNotes, DATE HireDate)
  236. {
  237.     //Convert the date to a dbVariant
  238.     COleVariant cdbHireDate;
  239.     cdbHireDate.date = HireDate;
  240.     cdbHireDate.vt = VT_DATE;
  241.  
  242.     try
  243.         {
  244.         //The recordset must be in edit mode
  245.         if(m_cEmpRecordSet.GetEditMode() == dbEditNone)
  246.             m_cEmpRecordSet.Edit();
  247.  
  248.         m_cEmpRecordSet.SetField(EMP_FIRST_NAME, COleVariant(lpszFirstName, VT_BSTRT));
  249.         m_cEmpRecordSet.SetField(EMP_HOME_PHONE, COleVariant(lpszHomePhone, VT_BSTRT));
  250.         m_cEmpRecordSet.SetField(EMP_LAST_NAME, COleVariant(lpszLastName, VT_BSTRT));
  251.         m_cEmpRecordSet.SetField(EMP_NOTES, COleVariant(lpszNotes, VT_BSTRT));
  252.         m_cEmpRecordSet.SetField(EMP_HIRE_DATE, cdbHireDate);
  253.  
  254.         //Commit the changes
  255.         m_cEmpRecordSet.Update();
  256.  
  257.         m_bEmptyTable = FALSE;
  258.  
  259.         //Return to the edited record
  260.         CdbBookmark cBookmark = m_cEmpRecordSet.GetLastModified();
  261.         m_cEmpRecordSet.SetBookmark(cBookmark);
  262.         }
  263.  
  264.     catch (CdbException e)
  265.         {
  266.         CdbLastOLEError exError;
  267.         TCHAR szBuf[256];
  268.  
  269.         wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
  270.         AfxMessageBox(szBuf);
  271.         }
  272. }
  273.  
  274. //Delete a record
  275. void CDAOEMPDoc::OnEditDelete() 
  276. {
  277.     //If table is empty, nothing to delete
  278.     if(m_bEmptyTable)
  279.         return;
  280.  
  281.     //Delete method depends on current mode
  282.     short nEditMode = m_cEmpRecordSet.GetEditMode();
  283.  
  284.     try
  285.         {
  286.         switch (nEditMode)
  287.             {
  288.             case dbEditNone: // Just delete it
  289.                 {
  290.                 m_cEmpRecordSet.Delete();
  291.                 m_cEmpRecordSet.MoveFirst();
  292.                 break;
  293.                 }
  294.     
  295.             case dbEditInProgress: //Forget changes
  296.                 {
  297.                 m_cEmpRecordSet.CancelUpdate();
  298.                 m_cEmpRecordSet.Delete();
  299.                 m_cEmpRecordSet.MoveFirst();
  300.                 break;
  301.                 }
  302.  
  303.             case dbEditAdd: //If new record, go back to last known
  304.                 {
  305.                 m_cEmpRecordSet.CancelUpdate();
  306.                 m_cEmpRecordSet.SetBookmark(m_cLastGoodRecord);
  307.                 }
  308.             }
  309.  
  310.         UpdateAllViews(NULL);
  311.         }
  312.  
  313.     catch (CdbException e)
  314.         {
  315.         CdbLastOLEError exError;
  316.         TCHAR szBuf[256];
  317.  
  318.         wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
  319.         AfxMessageBox(szBuf);
  320.         }
  321. }
  322.  
  323.  
  324.