int GetFieldPos(CDaoRecordset& recs, LPCTSTR fieldName) { CDaoFieldInfo fi; int count = recs.GetFieldCount(); for( int i = 0; i < count; i++ ) { recs.GetFieldInfo(i, fi); if( fi.m_strName.Compare( fieldName ) == 0 ) return i; // when found a match, return it } return -1; }
CDaoRecordset* CMyDaoDatabaseDoc::GetRecordset() { CDaoRecordset* pSet = new CCustSet(&m_db); try { pSet->Open(); } catch(CDaoException* pe) { AfxMessageBox(pe->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION); // Delete the incomplete recordset object delete pSet; pSet = NULL; pe->Delete(); } return pSet; }
//*************************************************************************** BOOL CBCGPDAOGridCtrl::GetFieldList (LPCTSTR lpszTable, CStringList& lstField) { ASSERT (lpszTable != NULL); lstField.RemoveAll (); try { if (m_pDataBase == NULL) { ASSERT (FALSE); return FALSE; } ASSERT_VALID (m_pDataBase); if (!m_pDataBase->IsOpen ()) { ASSERT (FALSE); return FALSE; } CDaoRecordset* pRecordSet = new CDaoRecordset (m_pDataBase); CString strSQL = _T("SELECT * FROM "); strSQL += lpszTable; pRecordSet->Open (AFX_DAO_USE_DEFAULT_TYPE, strSQL); if (!pRecordSet->IsOpen ()) { delete pRecordSet; return FALSE; } const short nColumns = pRecordSet->GetFieldCount (); for (short nColumn = 0; nColumn < nColumns; nColumn++) { CDaoFieldInfo info; pRecordSet->GetFieldInfo (nColumn, info); lstField.AddTail (info.m_strName); } pRecordSet->Close (); delete pRecordSet; } catch (CDaoException* pEx) { OnDaoException (pEx); pEx->Delete (); return FALSE; } return TRUE; }
void CDaoRecordView::OnInitialUpdate() { ASSERT_VALID(this); CDaoRecordset* pRecordset = OnGetRecordset(); // recordset must be allocated already ASSERT(pRecordset != NULL); if (!pRecordset->IsOpen()) { CWaitCursor wait; pRecordset->Open(); } if (!pRecordset->IsEOF()) { // Determine recordset properties for move button enabling if (pRecordset->CanBookmark()) { // Get the bookmark of the first record m_varBookmarkCurrent = pRecordset->GetBookmark(); m_varBookmarkFirst = m_varBookmarkCurrent; } // Enable forward scrolling buttons m_nStatus |= AFX_DAOVIEW_SCROLL_NEXT; // Enable backward scrolling buttons if possible if (pRecordset->CanScroll()) { m_nStatus |= AFX_DAOVIEW_SCROLL_LAST; m_nStatus |= AFX_DAOVIEW_SCROLL_BACKWARD; } else { m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; } } else { // Disable scrolling m_nStatus &= ~AFX_DAOVIEW_SCROLL_NEXT; m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; } CFormView::OnInitialUpdate(); }
BOOL CDaoRecordView::OnMove(UINT nIDMoveCommand) { ASSERT_VALID(this); CDaoRecordset* pSet = OnGetRecordset(); if (pSet->CanUpdate()) { pSet->Edit(); if (!UpdateData()) return TRUE; pSet->Update(); } BOOL bBookmarkable = pSet->CanBookmark(); BOOL bScrollable = pSet->CanScroll(); switch (nIDMoveCommand) { case ID_RECORD_PREV: pSet->MovePrev(); if (!pSet->IsBOF()) { if (bBookmarkable) m_varBookmarkCurrent = pSet->GetBookmark(); // Enable forward scrolling m_nStatus |= AFX_DAOVIEW_SCROLL_NEXT; if (bScrollable) { m_nStatus |= AFX_DAOVIEW_SCROLL_LAST; if (IsOnFirstRecord()) // Disable backward scrolling m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; else m_nStatus |= AFX_DAOVIEW_SCROLL_BACKWARD; } else { m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; } break; } // Fall through to reset to first record case ID_RECORD_FIRST: pSet->MoveFirst(); // backward scrolling never allowed after movefirst m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; if (pSet->IsEOF()) { // Empty recordset, disable forward too m_nStatus &= ~AFX_DAOVIEW_SCROLL_NEXT; m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; } else { if (bBookmarkable) { m_varBookmarkCurrent = pSet->GetBookmark(); m_varBookmarkFirst = m_varBookmarkCurrent; } // Enable forward scrolling m_nStatus |= AFX_DAOVIEW_SCROLL_NEXT; if (bScrollable) m_nStatus |= AFX_DAOVIEW_SCROLL_LAST; else m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; } break; case ID_RECORD_NEXT: pSet->MoveNext(); if (!pSet->IsEOF()) { if (bBookmarkable) m_varBookmarkCurrent = pSet->GetBookmark(); if (IsOnLastRecord()) { // Disable forward scrolling m_nStatus &= ~AFX_DAOVIEW_SCROLL_NEXT; m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; } else { m_nStatus |= AFX_DAOVIEW_SCROLL_NEXT; m_nStatus |= AFX_DAOVIEW_SCROLL_LAST; } if (bScrollable) m_nStatus |= AFX_DAOVIEW_SCROLL_BACKWARD; else { m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; } break; } // Can't fall through to move last if (!bScrollable) { // At the end of forward only recordset m_nStatus &= ~AFX_DAOVIEW_SCROLL_NEXT; m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; break; } // Fall through to reset to last record case ID_RECORD_LAST: pSet->MoveLast(); // forward scrolling never allowed after movelast m_nStatus &= ~AFX_DAOVIEW_SCROLL_NEXT; m_nStatus &= ~AFX_DAOVIEW_SCROLL_LAST; if (pSet->IsBOF()) { // Empty recordset, disable backward too m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; } else { if (bBookmarkable) { m_varBookmarkCurrent = pSet->GetBookmark(); m_varBookmarkLast = m_varBookmarkCurrent; } // Enable backward scrolling if (bBookmarkable) m_nStatus |= AFX_DAOVIEW_SCROLL_BACKWARD; else m_nStatus &= ~AFX_DAOVIEW_SCROLL_BACKWARD; } break; default: // Unexpected case value ASSERT(FALSE); } // Show results of move operation UpdateData(FALSE); return TRUE; }
// globale Funktion // richtiger Tabellentyp BOOL CheckTable (CDaoRecordset &Set, FieldKonvert *pInfo, const CString& strTableName) { try { CWaitCursor wc; CMutex mt; ASSERT (Set.IsOpen ()); ASSERT (NULL != pInfo); // Suche alle Felder CDaoFieldInfo FieldInfo; CString strError; while (pInfo && pInfo -> pFieldName && *pInfo -> pFieldName) { try { Set.GetFieldInfo (pInfo -> pFieldName, FieldInfo); } catch (CMemoryException *me) { me -> ReportError (); me -> Delete (); return FALSE; } catch (CDaoException *de) { de -> Delete (); AfxFormatString1 (strError, IDS_FIELD_NOT_FOUND, pInfo -> pFieldName); throw strError; } // Datentyp ok ? if (FieldInfo.m_nType != pInfo -> sType) { AfxFormatString1 (strError, IDS_FIELD_NO_TYPE, pInfo -> pFieldName); throw strError; } // ggf. auch Länge vergleichen if ((pInfo -> sLen > 0) && (pInfo -> sLen < FieldInfo.m_lSize)) // angeg. Länge muß kleiner gleich tatsächlicher sein { CString strLen; strLen.Format ("%ld", pInfo -> sLen); AfxFormatString2 (strError, IDS_FIELD_NO_LEN, pInfo -> pFieldName, strLen); throw strError; } pInfo++; } return TRUE; } catch (CString str) { // Struktur stimmt nicht: Meldung an Nutzer CString strOutput; AfxFormatString2 (strOutput, IDS_NO_VALID_STRUCTUR, strTableName, str); ((CGakApp *) AfxGetApp ()) -> OutputMessage (strOutput); } return FALSE; }