INFO: Example DDX Routine for Access Memo FieldsLast reviewed: September 11, 1997Article ID: Q119765 |
The information in this article applies to:
SUMMARYAn application, using the ODBC database classes of the Microsoft Foundation Classes, can create a CRecordset derived class object which associates a CString or a CLongBinary member variable with a memo field within an Access database. Technote #45, "MFC/Database Support for Long Varchar/Varbinary," available in the MFC Tech Notes helpfile in your Visual C++ program group discusses this. There are some limitations to using a CString object associated with a memo field, as opposed to a CLongBinary, though:
This article gives one example of such a DDX routine which reads data from a CLongBinary into an edit control and back. NOTE: A bug exists when using the 16-bit Access ODBC driver which is included with Visual C++ 1.52 which does not allow mapping of CLongBinary objects to Memo fields. For additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q130438 TITLE : BUG: Program Crashes When Memo Field Mapped to ClongBinary MORE INFORMATIONNote that the sample code below must explicitly call SetFieldNull() and SetFieldDirty() to set the field as NOT Null and as dirty when reading data from the control. Without both of these calls, the field will not be updated in the database. The DDX routine shown, will need to be explicitly called in your CFormView or CRecordView derived class's DoDataExchange() member function outside of the AFX_DATA_MAP section. For example the following code associates a CRecordset member variable CLongBinary m_lbMemo with an edit control with the ID IDC_EDIT_MEMO:
void CSampView::DoDataExchange(CDataExchange* pDX) { CRecordView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSampView) DDX_FieldText(pDX, IDC_EDIT_TITLE, m_pSet->m_strTitle, m_pSet); DDX_FieldText(pDX, IDC_EDIT_AREA, m_pSet->m_strArea, m_pSet); //}}AFX_DATA_MAP DDX_FieldMemo( pDX, IDC_EDIT_MEMO, m_pSet->m_lbMemo, m_pSet ); } REFERENCESMFC Tech Notes (helpfile icon in your Visual C++ program group)
Sample Code
/* Compile options needed: Default MFC application project options */ void DDX_FieldMemo( CDataExchange * pDX, int nIDEdit, CLongBinary & lbMemo, CRecordset * pRecordset ) { ASSERT_VALID( pRecordset ); UINT nLen; HGLOBAL hGlob; LPSTR lpStr; char * pStrWithNull; HWND hWndCtrl = pDX->PrepareEditCtrl(nIDEdit); if ( !hWndCtrl ) { ASSERT(FALSE); return; } if (pDX->m_bSaveAndValidate) { nLen = ::GetWindowTextLength(hWndCtrl); if ( nLen ) { hGlob = GlobalAlloc( GPTR | GMEM_SHARE, (DWORD)nLen ); if ( !hGlob ) { AfxThrowMemoryException(); } lpStr = (LPSTR)GlobalLock( hGlob ); if ( !lpStr ) { GlobalFree( hGlob ); AfxThrowMemoryException(); } // Allocate space for the '\0' string terminator // Throws exception if needed pStrWithNull = new char[nLen+1]; ::GetWindowText(hWndCtrl, pStrWithNull, nLen+1); // Cut off the null #ifndef _WIN32 _fmemcpy( lpStr, pStrWithNull, nLen ); #else memcpy( lpStr, pStrWithNull, nLen ); #endif delete [] pStrWithNull; GlobalUnlock( hGlob ); // Don't leave it locked. } else // Empty { nLen = 0; hGlob = NULL; } // Free memory we are replacing GlobalUnlock( lbMemo.m_hData ); GlobalFree( lbMemo.m_hData ); // Put in new data lbMemo.m_dwDataLength = (DWORD) nLen; lbMemo.m_hData = hGlob; if (nLen == 0) { if ( pRecordset->IsFieldNullable(&lbMemo) ) pRecordset->SetFieldNull( &lbMemo, TRUE); } else { // It is required that we explicitly set it Dirty // and NOT Null pRecordset->SetFieldNull( &lbMemo, FALSE ); pRecordset->SetFieldDirty( &lbMemo, TRUE ); } } else // Reading data from recordset into control { nLen = (UINT)lbMemo.m_dwDataLength; if ( nLen ) { lpStr = (LPSTR)GlobalLock( lbMemo.m_hData ); // Throws exception if needed pStrWithNull = new char[nLen+1]; #ifndef _WIN32 _fmemcpy( pStrWithNull, lpStr, nLen ); #else memcpy( pStrWithNull, lpStr, nLen ); #endif pStrWithNull[nLen] = 0; // Set '\0' at end of string SetWindowText( hWndCtrl, pStrWithNull ); delete [] pStrWithNull; GlobalUnlock( lbMemo.m_hData ); } else { SetWindowText( hWndCtrl, "" ); } } } |
Additional query words: SQL_LONGVARCHAR SQL_LONGVARBINARY
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |