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

书籍源码

开发平台:

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. ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  23. ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
  24. ON_EN_CHANGE(IDC_DEPARTMENTCODE, OnChangeDepartmentcode)
  25. ON_EN_CHANGE(IDC_DEPARTMENTNAME, OnChangeDepartmentname)
  26. ON_WM_DESTROY()
  27. //}}AFX_MSG_MAP
  28. // Standard printing commands
  29. ON_COMMAND(ID_FILE_PRINT, CDaoRecordView::OnFilePrint)
  30. ON_COMMAND(ID_FILE_PRINT_DIRECT, CDaoRecordView::OnFilePrint)
  31. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDaoRecordView::OnFilePrintPreview)
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CDAODepartmentView construction/destruction
  35. CDAODepartmentView::CDAODepartmentView()
  36. : CDaoRecordView(CDAODepartmentView::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CDAODepartmentView)
  39. m_pSet = NULL;
  40. m_FindDeptCode = _T("");
  41. //}}AFX_DATA_INIT
  42. m_bChangesMade = FALSE;
  43. }
  44. CDAODepartmentView::~CDAODepartmentView()
  45. {
  46. }
  47. void CDAODepartmentView::DoDataExchange(CDataExchange* pDX)
  48. {
  49. CDaoRecordView::DoDataExchange(pDX);
  50. //{{AFX_DATA_MAP(CDAODepartmentView)
  51. DDX_FieldText(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, m_pSet);
  52. DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
  53. DDX_FieldText(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, m_pSet);
  54. DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 50);
  55. DDX_Text(pDX, IDC_FINDCODE, m_FindDeptCode);
  56. DDV_MaxChars(pDX, m_FindDeptCode, 4);
  57. //}}AFX_DATA_MAP
  58. }
  59. BOOL CDAODepartmentView::PreCreateWindow(CREATESTRUCT& cs)
  60. {
  61. return CDaoRecordView::PreCreateWindow(cs);
  62. }
  63. void CDAODepartmentView::OnInitialUpdate()
  64. {
  65. m_pSet = &GetDocument()->m_dAODepartmentSet;
  66. CDaoRecordView::OnInitialUpdate();
  67. GetParentFrame()->RecalcLayout();
  68. ResizeParentToFit();
  69. if (m_pSet->m_pDatabase->CanTransact()) {
  70. m_pSet->Close(); //Close to start transaction
  71. m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
  72. m_pSet->Open(); //Open after starting transaction
  73. }
  74. }
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CDAODepartmentView printing
  77. BOOL CDAODepartmentView::OnPreparePrinting(CPrintInfo* pInfo)
  78. {
  79. // default preparation
  80. return DoPreparePrinting(pInfo);
  81. }
  82. void CDAODepartmentView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  83. {
  84. }
  85. void CDAODepartmentView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  86. {
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CDAODepartmentView diagnostics
  90. #ifdef _DEBUG
  91. void CDAODepartmentView::AssertValid() const
  92. {
  93. CDaoRecordView::AssertValid();
  94. }
  95. void CDAODepartmentView::Dump(CDumpContext& dc) const
  96. {
  97. CDaoRecordView::Dump(dc);
  98. }
  99. CDAODepartmentDoc* CDAODepartmentView::GetDocument() // non-debug version is inline
  100. {
  101. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDAODepartmentDoc)));
  102. return (CDAODepartmentDoc*)m_pDocument;
  103. }
  104. #endif //_DEBUG
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CDAODepartmentView database support
  107. CDaoRecordset* CDAODepartmentView::OnGetRecordset()
  108. {
  109. return m_pSet;
  110. }
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CDAODepartmentView message handlers
  113. //********* ADDED FOR DAODepartmentViewB ****************//
  114. BOOL CDAODepartmentView::OnMove(UINT nIDMoveCommand) 
  115. {
  116. SaveData();
  117. return CDaoRecordView::OnMove(nIDMoveCommand);
  118. }
  119. void CDAODepartmentView::OnFileNew() 
  120. {
  121. if (!m_pSet->IsBOF() 
  122. && !m_pSet->IsDeleted()
  123. && !m_pSet->IsEOF()) {
  124. //On a valid record
  125. UpdateData(TRUE); //Get data from dialog box
  126. if (m_pSet->GetEditMode() != dbEditAdd) {
  127. //Not currently adding a record
  128. m_pSet->Edit(); //Set for edit mode
  129. }
  130. m_pSet->Update(); //Update data if needed
  131. m_pSet->MoveLast(); //Get off record 1
  132. }
  133. m_pSet->SetFieldNull(NULL); //Clear all fields
  134. m_pSet->AddNew(); //Set database in AddNew mode
  135. UpdateData(FALSE); //Update dialog box fields
  136. }
  137. void CDAODepartmentView::OnRecordDeleterecord() 
  138. {
  139. if (AfxMessageBox( //Be sure to verify your deletes
  140. "Are you sure you want to delete?",
  141. MB_YESNO)
  142. != IDYES) {
  143. return;
  144. }
  145. if (m_pSet->GetEditMode() == dbEditAdd) {
  146. //Currently adding so don't delete, just cancel add.
  147. m_pSet->CancelUpdate();
  148. m_pSet->MovePrev();
  149. return;
  150. }
  151. try {
  152. m_pSet->Delete(); //Delete record
  153. }
  154. catch(CDaoException* e1) { //Failed
  155. TCHAR message[200];
  156. TCHAR display[255];
  157. e1->GetErrorMessage(message, 200);
  158. strcpy (display, "Delete Failed:n");
  159. strcat (display, message);
  160. AfxMessageBox( display, MB_ICONEXCLAMATION);
  161. m_pSet->MoveFirst(); //We lost our place.
  162. e1->Delete(); //Delete Error Message
  163. UpdateData(FALSE); //Update dialog box fields
  164. return;
  165. }
  166. m_pSet->MoveNext(); //Go to next record
  167. if (m_pSet->IsBOF() 
  168. || m_pSet->IsDeleted()
  169. || m_pSet->IsEOF()) {
  170. //Was there a next record?
  171. m_pSet->Requery(); //Reretrieve
  172. m_pSet->MoveFirst(); //Deleted last record
  173. }
  174. try {
  175. if (m_pSet->IsBOF() 
  176. || m_pSet->IsDeleted()
  177. || m_pSet->IsEOF()) {
  178. //Can't find a record
  179. AfxThrowDaoException();   
  180. }
  181. UpdateData(FALSE); //Update dialog box fields
  182. }
  183. catch(CDaoException* e2) { //No records exist
  184. AfxMessageBox("No more records",
  185. MB_ICONEXCLAMATION);
  186. e2->Delete(); //Delete Error Message
  187. //Close and Open to get rid of the Deleted record
  188. m_pSet->Close();
  189. m_pSet->Open();
  190. //No records, so set up an add record
  191. OnFileNew();
  192. }
  193. }
  194. void CDAODepartmentView::OnUpdateRecordDeleterecord(CCmdUI* pCmdUI) 
  195. {
  196. //Disable delete functionality if no record is found
  197. pCmdUI->Enable( //Enable delete if there's a record
  198. !m_pSet->IsBOF() &&
  199. !m_pSet->IsDeleted() &&
  200. !m_pSet->IsEOF());
  201. }
  202. void CDAODepartmentView::OnRecordQueryrecord() 
  203. {
  204. CString newFilter = ""; //Default is no filter
  205. UpdateData(TRUE); //Get data from dialog box
  206. if (m_pSet->GetEditMode() != dbEditAdd) {
  207. //Not currently adding, so set to update
  208. m_pSet->Edit();
  209. }
  210. m_pSet->Update(); //Update data if needed
  211. if (m_FindDeptCode != "") {
  212. //Setup new filter
  213. newFilter = "DepartmentCode = '" + m_FindDeptCode + "'";
  214. }
  215. if (newFilter != m_pSet->m_strFilter) {
  216. //Filter has changed
  217. m_pSet->m_strFilter = newFilter; //Assign new filter
  218. try {
  219. m_pSet->Requery(); //Requery
  220. }
  221. catch(CDaoException* e1)    {
  222. AfxMessageBox("Requery has failed");
  223. m_pSet->m_strFilter = ""; //Try to get back
  224. m_pSet->Requery(); //Requery again
  225. e1->Delete(); //Delete Error Message
  226. }
  227. try {
  228. //Go to the first record of the new filtered recordset
  229. m_pSet->MoveFirst();
  230. }
  231. catch(CDaoException* e2)    {
  232. //Move failed because there are no records
  233. AfxMessageBox("No records were found", MB_ICONEXCLAMATION );
  234. e2->Delete(); //Delete Error Message
  235. //No records, so set up an add record
  236. OnFileNew();
  237. }
  238. }
  239. UpdateData(FALSE); //Update dialog box fields
  240. }
  241. //********* ADDED FOR DAODepartmentViewC ****************//
  242. void CDAODepartmentView::OnChangeDepartmentcode() 
  243. {
  244. m_bChangesMade = TRUE;
  245. }
  246. void CDAODepartmentView::OnChangeDepartmentname() 
  247. {
  248. m_bChangesMade = TRUE;
  249. }
  250. void CDAODepartmentView::OnFileSave() 
  251. {
  252. SaveData();
  253. if (m_pSet->m_pDatabase->CanTransact() 
  254. && m_bChangesMade) {
  255. m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();
  256. m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
  257. }
  258. m_bChangesMade = FALSE;
  259. }
  260. void CDAODepartmentView::OnUpdateFileSave(CCmdUI* pCmdUI) 
  261. {
  262. //Enable save if records have been changed
  263. pCmdUI->Enable(m_bChangesMade && 
  264. m_pSet->m_pDatabase->CanTransact());
  265. }
  266. void CDAODepartmentView::OnDestroy() 
  267. {
  268. //Check for changed fields and transaciton ability
  269. if (m_pSet->m_pDatabase->CanTransact() 
  270. && m_bChangesMade) {
  271. if (AfxMessageBox (
  272. "Records have been changed.  Do you want to save?", 
  273. MB_YESNO) == IDYES) {
  274. SaveData();
  275. OnFileSave();
  276. }
  277. }
  278. //Roll back last transaction
  279. m_pSet->m_pDatabase->m_pWorkspace->Rollback();
  280. CDaoRecordView::OnDestroy();
  281. }
  282. void CDAODepartmentView::SaveData() 
  283. {
  284. if (m_pSet->GetEditMode() != dbEditAdd) {
  285. //Currently not adding a record, so set edit mode
  286. m_pSet->Edit();
  287. }
  288. UpdateData(TRUE); //Get data from dialog box
  289. m_pSet->Update(); //Update data if needed
  290. }