DAODepartmentView.cpp
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // DAODepartmentView.cpp : implementation of the CDAODepartmentView class
  2. //
  3. #include "stdafx.h"
  4. #include "DAODepartment.h"
  5. #include "DAODepartmentSet.h"
  6. #include "DAODepartmentDoc.h"
  7. #include "DAODepartmentView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CDAODepartmentView
  15. IMPLEMENT_DYNCREATE(CDAODepartmentView, CDaoRecordView)
  16. BEGIN_MESSAGE_MAP(CDAODepartmentView, CDaoRecordView)
  17. //{{AFX_MSG_MAP(CDAODepartmentView)
  18. ON_COMMAND(ID_FILE_NEW, OnFileNew)
  19. ON_COMMAND(ID_RECORD_DELETERECORD, OnRecordDeleterecord)
  20. ON_UPDATE_COMMAND_UI(ID_RECORD_DELETERECORD, OnUpdateRecordDeleterecord)
  21. ON_COMMAND(ID_RECORD_QUERYRECORD, OnRecordQueryrecord)
  22. //}}AFX_MSG_MAP
  23. // Standard printing commands
  24. ON_COMMAND(ID_FILE_PRINT, CDaoRecordView::OnFilePrint)
  25. ON_COMMAND(ID_FILE_PRINT_DIRECT, CDaoRecordView::OnFilePrint)
  26. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDaoRecordView::OnFilePrintPreview)
  27. END_MESSAGE_MAP()
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CDAODepartmentView construction/destruction
  30. CDAODepartmentView::CDAODepartmentView()
  31. : CDaoRecordView(CDAODepartmentView::IDD)
  32. {
  33. //{{AFX_DATA_INIT(CDAODepartmentView)
  34. m_pSet = NULL;
  35. m_FindDeptCode = _T("");
  36. //}}AFX_DATA_INIT
  37. }
  38. CDAODepartmentView::~CDAODepartmentView()
  39. {
  40. }
  41. void CDAODepartmentView::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDaoRecordView::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CDAODepartmentView)
  45. DDX_FieldText(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, m_pSet);
  46. DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
  47. DDX_FieldText(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, m_pSet);
  48. DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 50);
  49. DDX_Text(pDX, IDC_FINDCODE, m_FindDeptCode);
  50. DDV_MaxChars(pDX, m_FindDeptCode, 4);
  51. //}}AFX_DATA_MAP
  52. }
  53. BOOL CDAODepartmentView::PreCreateWindow(CREATESTRUCT& cs)
  54. {
  55. return CDaoRecordView::PreCreateWindow(cs);
  56. }
  57. void CDAODepartmentView::OnInitialUpdate()
  58. {
  59. m_pSet = &GetDocument()->m_dAODepartmentSet;
  60. CDaoRecordView::OnInitialUpdate();
  61. GetParentFrame()->RecalcLayout();
  62. ResizeParentToFit();
  63. }
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CDAODepartmentView printing
  66. BOOL CDAODepartmentView::OnPreparePrinting(CPrintInfo* pInfo)
  67. {
  68. // default preparation
  69. return DoPreparePrinting(pInfo);
  70. }
  71. void CDAODepartmentView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  72. {
  73. }
  74. void CDAODepartmentView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  75. {
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CDAODepartmentView diagnostics
  79. #ifdef _DEBUG
  80. void CDAODepartmentView::AssertValid() const
  81. {
  82. CDaoRecordView::AssertValid();
  83. }
  84. void CDAODepartmentView::Dump(CDumpContext& dc) const
  85. {
  86. CDaoRecordView::Dump(dc);
  87. }
  88. CDAODepartmentDoc* CDAODepartmentView::GetDocument() // non-debug version is inline
  89. {
  90. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDAODepartmentDoc)));
  91. return (CDAODepartmentDoc*)m_pDocument;
  92. }
  93. #endif //_DEBUG
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CDAODepartmentView database support
  96. CDaoRecordset* CDAODepartmentView::OnGetRecordset()
  97. {
  98. return m_pSet;
  99. }
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CDAODepartmentView message handlers
  102. BOOL CDAODepartmentView::OnMove(UINT nIDMoveCommand) 
  103. {
  104. if (m_pSet->GetEditMode() == dbEditAdd) {
  105. //Currently adding a record
  106. //So update the record before the move
  107. UpdateData(TRUE); //Get data from dialog box
  108. m_pSet->Update(); //Update data if needed
  109. m_pSet->MoveLast(); //Go to the added record
  110. }
  111. //Continue with normal processing
  112. return CDaoRecordView::OnMove(nIDMoveCommand);
  113. }
  114. void CDAODepartmentView::OnFileNew() 
  115. {
  116. if (!m_pSet->IsBOF() 
  117. && !m_pSet->IsDeleted()
  118. && !m_pSet->IsEOF()) {
  119. //On a valid record
  120. UpdateData(TRUE); //Get data from dialog box
  121. if (m_pSet->GetEditMode() != dbEditAdd) {
  122. //Not currently adding a record
  123. m_pSet->Edit(); //Set for edit mode
  124. }
  125. m_pSet->Update(); //Update data if needed
  126. m_pSet->MoveLast(); //Get off record 1
  127. }
  128. m_pSet->SetFieldNull(NULL); //Clear all fields
  129. m_pSet->AddNew(); //Set database in AddNew mode
  130. UpdateData(FALSE); //Update dialog box fields
  131. }
  132. void CDAODepartmentView::OnRecordDeleterecord() 
  133. {
  134. if (AfxMessageBox( //Be sure to verify your deletes
  135. "Are you sure you want to delete?",
  136. MB_YESNO)
  137. != IDYES) {
  138. return;
  139. }
  140. if (m_pSet->GetEditMode() == dbEditAdd) {
  141. //Currently adding so don't delete, just cancel add.
  142. m_pSet->CancelUpdate();
  143. m_pSet->MovePrev();
  144. return;
  145. }
  146. try {
  147. m_pSet->Delete(); //Delete record
  148. }
  149. catch(CDaoException* e1) { //Failed
  150. TCHAR message[200];
  151. TCHAR display[255];
  152. e1->GetErrorMessage(message, 200);
  153. strcpy (display, "Delete Failed:n");
  154. strcat (display, message);
  155. AfxMessageBox( display, MB_ICONEXCLAMATION);
  156. m_pSet->MoveFirst(); //We lost our place.
  157. e1->Delete(); //Delete Error Message
  158. UpdateData(FALSE); //Update dialog box fields
  159. return;
  160. }
  161. m_pSet->MoveNext(); //Go to next record
  162. if (m_pSet->IsBOF() 
  163. || m_pSet->IsDeleted()
  164. || m_pSet->IsEOF()) {
  165. //Was there a next record?
  166. m_pSet->Requery(); //Reretrieve
  167. m_pSet->MoveFirst(); //Deleted last record
  168. }
  169. try {
  170. if (m_pSet->IsBOF() 
  171. || m_pSet->IsDeleted()
  172. || m_pSet->IsEOF()) {
  173. //Can't find a record
  174. AfxThrowDaoException();   
  175. }
  176. UpdateData(FALSE); //Update dialog box fields
  177. }
  178. catch(CDaoException* e2) { //No records exist
  179. AfxMessageBox("No more records",
  180. MB_ICONEXCLAMATION);
  181. e2->Delete(); //Delete Error Message
  182. //Close and Open to get rid of the Deleted record
  183. m_pSet->Close();
  184. m_pSet->Open();
  185. //No records, so set up an add record
  186. OnFileNew();
  187. }
  188. }
  189. void CDAODepartmentView::OnUpdateRecordDeleterecord(CCmdUI* pCmdUI) 
  190. {
  191. //Disable delete functionality if no record is found
  192. pCmdUI->Enable( //Enable delete if there's a record
  193. !m_pSet->IsBOF() &&
  194. !m_pSet->IsDeleted() &&
  195. !m_pSet->IsEOF());
  196. }
  197. void CDAODepartmentView::OnRecordQueryrecord() 
  198. {
  199. CString newFilter = ""; //Default is no filter
  200. UpdateData(TRUE); //Get data from dialog box
  201. if (m_pSet->GetEditMode() != dbEditAdd) {
  202. //Not currently adding, so set to update
  203. m_pSet->Edit();
  204. }
  205. m_pSet->Update(); //Update data if needed
  206. if (m_FindDeptCode != "") {
  207. //Setup new filter
  208. newFilter = "DepartmentCode = '" + m_FindDeptCode + "'";
  209. }
  210. if (newFilter != m_pSet->m_strFilter) {
  211. //Filter has changed
  212. m_pSet->m_strFilter = newFilter; //Assign new filter
  213. try {
  214. m_pSet->Requery(); //Requery
  215. }
  216. catch(CDaoException* e1)    {
  217. AfxMessageBox("Requery has failed");
  218. m_pSet->m_strFilter = ""; //Try to get back
  219. m_pSet->Requery(); //Requery again
  220. e1->Delete(); //Delete Error Message
  221. }
  222. try {
  223. //Go to the first record of the new filtered recordset
  224. m_pSet->MoveFirst();
  225. }
  226. catch(CDaoException* e2)    {
  227. //Move failed because there are no records
  228. AfxMessageBox("No records were found", MB_ICONEXCLAMATION );
  229. e2->Delete(); //Delete Error Message
  230. //No records, so set up an add record
  231. OnFileNew();
  232. }
  233. }
  234. UpdateData(FALSE); //Update dialog box fields
  235. }