home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c08 / querydef / qdefview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  5.4 KB  |  202 lines

  1. // QueryDefView.cpp : implementation of the CQueryDefView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "QueryDef.h"
  6. #include "QDefYDlg.h"
  7. #include "QDefSet.h"
  8. #include "QDefDoc.h"
  9. #include "QDefView.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CQueryDefView
  19.  
  20. IMPLEMENT_DYNCREATE(CQueryDefView, CDaoRecordView)
  21.  
  22. BEGIN_MESSAGE_MAP(CQueryDefView, CDaoRecordView)
  23.     //{{AFX_MSG_MAP(CQueryDefView)
  24.     ON_COMMAND(ID_QUERYDEF_CREATE, OnQuerydefCreate)
  25.     ON_COMMAND(ID_QUERYDEF_EXECUTE, OnQuerydefExecute)
  26.     ON_UPDATE_COMMAND_UI(ID_QUERYDEF_EXECUTE, OnUpdateQuerydefExecute)
  27.     ON_COMMAND(ID_QUERYDEF_REMOVE, OnQuerydefRemove)
  28.     ON_UPDATE_COMMAND_UI(ID_QUERYDEF_REMOVE, OnUpdateQuerydefRemove)
  29.     ON_UPDATE_COMMAND_UI(ID_QUERYDEF_CREATE, OnUpdateQuerydefCreate)
  30.     ON_WM_DESTROY()
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CQueryDefView construction/destruction
  36.  
  37. CQueryDefView::CQueryDefView()
  38.     : CDaoRecordView(CQueryDefView::IDD)
  39. {
  40.     //{{AFX_DATA_INIT(CQueryDefView)
  41.     m_pSet = NULL;
  42.     //}}AFX_DATA_INIT
  43.     // TODO: add construction code here
  44.     m_pQueryDef = NULL;
  45.     m_nYear = 1994; 
  46. }
  47.  
  48. CQueryDefView::~CQueryDefView()
  49. {
  50. }
  51.  
  52. void CQueryDefView::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CDaoRecordView::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CQueryDefView)
  56.     DDX_FieldText(pDX, IDC_TITLE, m_pSet->m_Title, m_pSet);
  57.     DDX_FieldText(pDX, IDC_ISBN, m_pSet->m_ISBN, m_pSet);
  58.     DDX_FieldText(pDX, IDC_YEAR_PUBLISHED, m_pSet->m_Year_Published, m_pSet);
  59.     DDX_FieldText(pDX, IDC_SUBJECT, m_pSet->m_Subject, m_pSet);
  60.     DDX_FieldText(pDX, IDC_NOTES, m_pSet->m_Notes, m_pSet);
  61.     DDX_FieldText(pDX, IDC_COMMENTS, m_pSet->m_Comments, m_pSet);
  62.     DDX_FieldText(pDX, IDC_DESCRIPTION, m_pSet->m_Description, m_pSet);
  63.     DDX_FieldText(pDX, IDC_PUBID, m_pSet->m_PubID, m_pSet);
  64.     //}}AFX_DATA_MAP
  65. }
  66.  
  67. BOOL CQueryDefView::PreCreateWindow(CREATESTRUCT& cs)
  68. {
  69.     // TODO: Modify the Window class or styles here by modifying
  70.     //  the CREATESTRUCT cs
  71.  
  72.     return CDaoRecordView::PreCreateWindow(cs);
  73. }
  74.  
  75. void CQueryDefView::OnInitialUpdate()
  76. {
  77.     m_pSet = &GetDocument()->m_queryDefSet;
  78.     CDaoRecordView::OnInitialUpdate();
  79.  
  80.     GetParentFrame()->RecalcLayout();
  81.     ResizeParentToFit();
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CQueryDefView diagnostics
  86.  
  87. #ifdef _DEBUG
  88. void CQueryDefView::AssertValid() const
  89. {
  90.     CDaoRecordView::AssertValid();
  91. }
  92.  
  93. void CQueryDefView::Dump(CDumpContext& dc) const
  94. {
  95.     CDaoRecordView::Dump(dc);
  96. }
  97.  
  98. CQueryDefDoc* CQueryDefView::GetDocument() // non-debug version is inline
  99. {
  100.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQueryDefDoc)));
  101.     return (CQueryDefDoc*)m_pDocument;
  102. }
  103. #endif //_DEBUG
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CQueryDefView database support
  107. CDaoRecordset* CQueryDefView::OnGetRecordset()
  108. {
  109.     return m_pSet;
  110. }
  111.  
  112.  
  113. /////////////////////////////////////////////////////////////////////////////
  114. // CQueryDefView message handlers
  115.  
  116. void CQueryDefView::OnQuerydefCreate() 
  117. {
  118.     // This is a fixed query that allows the user to specify a date for
  119.     // viewing the titles in the database
  120.     // Create the PARAMETERS string
  121.     CString strParam = "PARAMETERS [Year Desired] Integer; ";
  122.     // Create the query definition prefixing it with the PARAMETERS string
  123.     CString strSQL = strParam + "SELECT * FROM Titles WHERE "
  124.         "[Year Published] = [Year Desired]";
  125.  
  126.     // Create a QueryDef object using the database from the recordset
  127.     m_pQueryDef = new CDaoQueryDef(m_pSet->m_pDatabase);
  128.  
  129.     // Create the underlying QueryDef object and give it a name. Because this
  130.     // is to be a temporary object, the name will be NULL.
  131.  
  132.     m_pQueryDef->Create(NULL, strSQL);
  133.     // To make this QueryDef persistent, you would call Append to add
  134.     // it to the database's collection. Not calling CDaoQueryDef.Append 
  135.     // allows it to be used as a temporary object
  136.     //m_pQueryDef->Append();
  137. }
  138.  
  139. void CQueryDefView::OnUpdateQuerydefCreate(CCmdUI* pCmdUI) 
  140. {
  141.         pCmdUI->Enable(!m_pQueryDef);    
  142. }
  143.  
  144. void CQueryDefView::OnQuerydefExecute() 
  145. {
  146.     // Execute the QueryDef for the desired year
  147.     ASSERT(m_pQueryDef->IsOpen());
  148.  
  149.     CQDYearDlg dlg;
  150.     dlg.m_nYear = m_nYear;
  151.     if(IDOK == dlg.DoModal())
  152.     {
  153.         // Get the desired year
  154.         m_nYear = dlg.m_nYear;
  155.  
  156.         // Set the QueryDef's parameter values. Note that you pass it as
  157.         // a COleVariant.
  158.         m_pQueryDef->SetParamValue("[Year Desired]", COleVariant(m_nYear));
  159.  
  160.         // Close and reopen the recordset based upon the QueryDef and new
  161.         // parameter.
  162.         m_pSet->Close();
  163.         m_pSet->Open(m_pQueryDef, dbOpenDynaset);
  164.         
  165.         if(m_pSet->IsEOF())
  166.         {
  167.             // Empty Recordset. Mark fields as NULL.
  168.             m_pSet->SetFieldNull(NULL, TRUE);
  169.         }
  170.         UpdateData(FALSE);
  171.     }
  172. }
  173.  
  174. void CQueryDefView::OnUpdateQuerydefExecute(CCmdUI* pCmdUI) 
  175. {
  176.     pCmdUI->Enable(m_pQueryDef && m_pQueryDef->IsOpen());    
  177. }
  178.  
  179. void CQueryDefView::OnQuerydefRemove() 
  180. {
  181.     ASSERT(m_pQueryDef);
  182.     if(m_pQueryDef->IsOpen())
  183.         m_pQueryDef->Close();
  184.     delete m_pQueryDef;
  185.     m_pQueryDef = NULL;
  186. }
  187.  
  188. void CQueryDefView::OnUpdateQuerydefRemove(CCmdUI* pCmdUI) 
  189. {
  190.     pCmdUI->Enable(m_pQueryDef && m_pQueryDef->IsOpen());    
  191. }
  192.  
  193.  
  194. void CQueryDefView::OnDestroy() 
  195. {
  196.     CDaoRecordView::OnDestroy();
  197.     
  198.     // Cleanup before terminating
  199.     if (NULL != m_pQueryDef)
  200.         OnQuerydefRemove();
  201. }
  202.