// brdbworksView.cpp : implementation of the CBrdbworksView class // #include "stdafx.h" #include "brdbworks.h" #include "brdbworksSet.h" #include "brdbworksDoc.h" #include "brdbworksView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CBrdbworksView IMPLEMENT_DYNCREATE(CBrdbworksView, CRecordView) BEGIN_MESSAGE_MAP(CBrdbworksView, CRecordView) //{{AFX_MSG_MAP(CBrdbworksView) ON_COMMAND(ID_ADD_RECORD, OnAddRecord) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBrdbworksView construction/destruction CBrdbworksView::CBrdbworksView() : CRecordView(CBrdbworksView::IDD) { //{{AFX_DATA_INIT(CBrdbworksView) m_pSet = NULL; //}}AFX_DATA_INIT // TODO: add construction code here m_bAddMode = FALSE; // "Adding data" flag used in OnAddRecord() // and OnMove() functions } CBrdbworksView::~CBrdbworksView() { } void CBrdbworksView::DoDataExchange(CDataExchange* pDX) { CRecordView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CBrdbworksView) DDX_FieldText(pDX, IDC_AUTHOR, m_pSet->m_author, m_pSet); DDX_FieldText(pDX, IDC_AUTHOR_ID, m_pSet->m_au_id, m_pSet); DDX_FieldText(pDX, IDC_YEAR_BORN, m_pSet->m_year_born, m_pSet); //}}AFX_DATA_MAP } BOOL CBrdbworksView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CRecordView::PreCreateWindow(cs); } void CBrdbworksView::OnInitialUpdate() { m_pSet = &GetDocument()->m_brdbworksSet; CRecordView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); } ///////////////////////////////////////////////////////////////////////////// // CBrdbworksView printing BOOL CBrdbworksView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CBrdbworksView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CBrdbworksView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CBrdbworksView diagnostics #ifdef _DEBUG void CBrdbworksView::AssertValid() const { CRecordView::AssertValid(); } void CBrdbworksView::Dump(CDumpContext& dc) const { CRecordView::Dump(dc); } CBrdbworksDoc* CBrdbworksView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBrdbworksDoc))); return (CBrdbworksDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CBrdbworksView database support CRecordset* CBrdbworksView::OnGetRecordset() { return m_pSet; } ///////////////////////////////////////////////////////////////////////////// // CBrdbworksView message handlers void CBrdbworksView::OnAddRecord() { // If already in add mode, complete the previous new record if (m_bAddMode) OnMove(ID_RECORD_FIRST); m_pSet->AddNew(); // Blank out the variables // // IMPORTANT NOTE: you must put some kind of value into at // least one of the fields (for example, a // zero in a numeric field or maybe the // constant "" into a CString field). // If all of the fields have a NULL value, // the ODBC driver will not add the new row // to the database (i.e., it will not add // a row full of NULLs to the table). // m_pSet->SetFieldNull(&(m_pSet->m_au_id), FALSE); m_pSet->SetFieldNull(&(m_pSet->m_author), FALSE); m_pSet->m_year_born=0; UpdateData(FALSE); // reinitialize/repaint the dialog box m_bAddMode = TRUE; // set "adding data" flag to refer to // in the OnMove() function } // // To add the function shown below, you must go into the class // wizard, view the "Message Maps" tab, select your "View" class, // and scroll the "Messages" section down to the "OnMove" message. // Click once on "OnMove" to select it, then click on the // "Add Function" pushbutton. Once you have added the function, // you can click on the "Edit Code" pushbutton to edit the // body of the function to incorporate the code shown below. // BOOL CBrdbworksView::OnMove(UINT nIDMoveCommand) { if (m_bAddMode) { if (!UpdateData()) // retrieve data from dialog box return FALSE; // but don't add an empty record // to the database TRY { m_pSet->Update(); // post new row to the database } CATCH(CDBException, e) { AfxMessageBox(e->m_strError); return FALSE; } END_CATCH m_pSet->Requery(); // refresh the dynaset/query UpdateData(FALSE); // reinitialize/repaint the dialog box m_bAddMode = FALSE; // set the "Adding data" flag to false return TRUE; } else { /* if not adding data, call the generic OnMove function */ return CRecordView::OnMove(nIDMoveCommand); } }