home *** CD-ROM | disk | FTP | other *** search
- // DAOEMdoc.cpp : implementation of the CDAOEMPDoc class
- //
- // The document in this example holds the database connection
-
-
- #include "stdafx.h"
- #include "DAOEMP.h"
-
- #include "DAOEMdoc.h"
- #include "DAOEMvw.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- /*****************************************************************************
- * Forwards
- */
- class CDAOEMPView;
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOEMPDoc
-
- IMPLEMENT_DYNCREATE(CDAOEMPDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CDAOEMPDoc, CDocument)
- //{{AFX_MSG_MAP(CDAOEMPDoc)
- ON_COMMAND(ID_EDIT_NEXT, OnEditNext)
- ON_COMMAND(ID_EDIT_PREVIOUS, OnEditPrevious)
- ON_COMMAND(ID_EDIT_ADD, OnEditAdd)
- ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOEMPDoc construction/destruction
-
- CDAOEMPDoc::CDAOEMPDoc()
- {
-
- }
-
- CDAOEMPDoc::~CDAOEMPDoc()
- {
- }
-
- BOOL CDAOEMPDoc::OnNewDocument()
- {
- if (!COleDocument::OnNewDocument())
- return FALSE;
-
- //Assume it's not empty
- m_bEmptyTable = FALSE;
-
- //Ask the user to point to the EMPLOYEE database
- if(!ConnectToDatabase())
- {
- m_bConnected = FALSE;
- return FALSE;
- }
-
- m_bConnected = TRUE;
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOEMPDoc serialization
-
- void CDAOEMPDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOEMPDoc diagnostics
-
- #ifdef _DEBUG
- void CDAOEMPDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void CDAOEMPDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CDAOEMPDoc support
-
- //When the document is created, connect to the database and open the
- //Employee recordset.
- BOOL CDAOEMPDoc::ConnectToDatabase()
- {
- CFileDialog cOpenFile( TRUE,
- _T("MDB"),
- _T("employee.mdb"),
- OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- (_T("Access Files (*.mdb) | *.mdb ||")));
-
-
-
- //Get the location of database (assume it's the Employee example)
- cOpenFile.DoModal();
-
- //Open the database and the recordset
- try
- {
- //NOTE: Using default collection rather than Workspaces.Items
- m_cEmpDatabase = m_cDBEngine.OpenDatabase(cOpenFile.m_ofn.lpstrFile);
- m_cEmpRecordSet = m_cEmpDatabase.OpenRecordset(_T("Employees"));
- m_cEmpRecordSet.MoveFirst();
- }
-
- catch (CdbException dbExcept)
- {
- CdbLastOLEError exError;
- TCHAR szBuf[256];
-
- //If error is "No Current Record" assume it's an empty table
- if(dbExcept.m_hr == E_DAO_NoCurrentRecord)
- {
- m_bEmptyTable = TRUE;
- }
- else
- {
- wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbExcept.m_hr), (LPCTSTR) exError.GetDescription());
- AfxMessageBox(szBuf);
- return (FALSE);
- }
- }
-
-
- return TRUE;
- }
-
-
- //Move to the next record
- void CDAOEMPDoc::OnEditNext()
- {
- if(!OKToMove())
- return;
-
- m_cEmpRecordSet.MoveNext();
-
- //Watch for end of record set
- if(m_cEmpRecordSet.GetEOF())
- {
- MessageBeep(0);
- m_cEmpRecordSet.MovePrevious();
- }
- else
- {
- UpdateAllViews(NULL);
- }
-
- }
-
- //Move to Previous record
- void CDAOEMPDoc::OnEditPrevious()
- {
- if(!OKToMove())
- return;
-
- m_cEmpRecordSet.MovePrevious();
-
- //Watch for beginning of recordset
- if(m_cEmpRecordSet.GetBOF())
- {
- MessageBeep(0);
- m_cEmpRecordSet.MoveNext();
- }
- else
- {
- UpdateAllViews(NULL);
- }
- }
-
-
- //Add a new record
- void CDAOEMPDoc::OnEditAdd()
- {
- //If the table is empty, add a blank record
- if(m_bEmptyTable)
- {
- m_cEmpRecordSet.AddNew();
- m_cEmpRecordSet.Update();
- m_cEmpRecordSet.MoveFirst();
- m_bEmptyTable = FALSE;
- return;
- }
- else
- {
- if(!OKToMove())
- return;
-
- //Remember where we were before adding in case the user
- //cancels and we have to return
- m_cLastGoodRecord = m_cEmpRecordSet.GetBookmark();
- m_cEmpRecordSet.AddNew();
- m_cEmpRecordSet.Fields[EMP_HIRE_DATE].SetValue(COleVariant(32874L)); // Default to 1/1/90
- }
-
-
- UpdateAllViews(NULL);
- }
-
-
- //Is it OK to move to a different record(i.e. do we have "dirty" that need updating
- BOOL CDAOEMPDoc::OKToMove()
- {
- POSITION pos = GetFirstViewPosition();
- CDAOEMPView* pView = (CDAOEMPView*)GetNextView(pos);
-
- if(m_bEmptyTable)
- return FALSE;
-
- return pView->CommitAlteredEmpRec();
- }
-
-
- //Update
- void CDAOEMPDoc::UpdateEmpRec(long m_nEmpNum, LPCTSTR lpszFirstName,
- LPCTSTR lpszHomePhone, LPCTSTR lpszLastName,
- LPCTSTR lpszNotes, DATE HireDate)
- {
- //Convert the date to a dbVariant
- COleVariant cdbHireDate;
- cdbHireDate.date = HireDate;
- cdbHireDate.vt = VT_DATE;
-
- try
- {
- //The recordset must be in edit mode
- if(m_cEmpRecordSet.GetEditMode() == dbEditNone)
- m_cEmpRecordSet.Edit();
-
- m_cEmpRecordSet.SetField(EMP_FIRST_NAME, COleVariant(lpszFirstName, VT_BSTRT));
- m_cEmpRecordSet.SetField(EMP_HOME_PHONE, COleVariant(lpszHomePhone, VT_BSTRT));
- m_cEmpRecordSet.SetField(EMP_LAST_NAME, COleVariant(lpszLastName, VT_BSTRT));
- m_cEmpRecordSet.SetField(EMP_NOTES, COleVariant(lpszNotes, VT_BSTRT));
- m_cEmpRecordSet.SetField(EMP_HIRE_DATE, cdbHireDate);
-
- //Commit the changes
- m_cEmpRecordSet.Update();
-
- m_bEmptyTable = FALSE;
-
- //Return to the edited record
- CdbBookmark cBookmark = m_cEmpRecordSet.GetLastModified();
- m_cEmpRecordSet.SetBookmark(cBookmark);
- }
-
- catch (CdbException e)
- {
- CdbLastOLEError exError;
- TCHAR szBuf[256];
-
- wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
- AfxMessageBox(szBuf);
- }
- }
-
- //Delete a record
- void CDAOEMPDoc::OnEditDelete()
- {
- //If table is empty, nothing to delete
- if(m_bEmptyTable)
- return;
-
- //Delete method depends on current mode
- short nEditMode = m_cEmpRecordSet.GetEditMode();
-
- try
- {
- switch (nEditMode)
- {
- case dbEditNone: // Just delete it
- {
- m_cEmpRecordSet.Delete();
- m_cEmpRecordSet.MoveFirst();
- break;
- }
-
- case dbEditInProgress: //Forget changes
- {
- m_cEmpRecordSet.CancelUpdate();
- m_cEmpRecordSet.Delete();
- m_cEmpRecordSet.MoveFirst();
- break;
- }
-
- case dbEditAdd: //If new record, go back to last known
- {
- m_cEmpRecordSet.CancelUpdate();
- m_cEmpRecordSet.SetBookmark(m_cLastGoodRecord);
- }
- }
-
- UpdateAllViews(NULL);
- }
-
- catch (CdbException e)
- {
- CdbLastOLEError exError;
- TCHAR szBuf[256];
-
- wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
- AfxMessageBox(szBuf);
- }
- }
-
-
-