Click here to Skip to main content
15,500,741 members
Articles / Desktop Programming / MFC
Posted 20 Feb 2001


67 bookmarked

Using the CDatabase class to read an Access databases

Rate me:
Please Sign up or sign in to vote.
4.71/5 (41 votes)
29 Mar 2001
This is a very simple code snippet that demonstrates how to read a Microsoft Access database file using the CDatabase class

Sample Image - ReadDB.gif


This is a very simple code snippet that demonstrates how to read a Microsoft Access database using the CDatabase class.

The main features it demonstrates are:

  • Retrieving data from Microsoft Access database
  • Connecting without the need for an ODBC data source to be set up.
  • Populate a List view Control with the data
// Part of the source code
void CReadDBDlg::OnRead() 
	// TODO: Add your control notification handler code here
	CDatabase database;
	CString SqlString;
	CString sCatID, sCategory;
	CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
	CString sDsn;
	CString sFile = "d:\\works\\ReadDB\\Test.mdb";
	// You must change above path if it's different
	int iRec = 0; 	
	// Build ODBC connection string
		// Open the database
		// Allocate the recordset
		CRecordset recset( &database );

		// Build the SQL statement
		SqlString =  "SELECT CatID, Category "
				"FROM Categories";

		// Execute the query
		// Reset List control if there is any data
		// populate Grids
		// Column width and heading
		m_ListControl.InsertColumn(0,"Category Id",LVCFMT_LEFT,-1,0);
		m_ListControl.SetColumnWidth(0, 120);
		m_ListControl.SetColumnWidth(1, 200);

		// Loop through each record
		while( !recset.IsEOF() )
			// Copy each column into a variable

			// Insert values into the list control
			iRec = m_ListControl.InsertItem(0,sCatID,0);

			// goto next record
		// Close the database
	CATCH(CDBException, e)
		// If a database exception occured, show error msg
		AfxMessageBox("Database error: "+e->m_strError);
// Reset List control
void CReadDBDlg::ResetListControl()
	int iNbrOfColumns;
	CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
	if (pHeader)
		iNbrOfColumns = pHeader->GetItemCount();
	for (int i = iNbrOfColumns; i >= 0; i--)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionx64? Pin
gok20-Dec-16 5:38
professionalgok20-Dec-16 5:38 
QuestionCharacter truncation if switching to Unicode in Memo fields Pin
dlavrantonis15-Oct-13 5:11
Memberdlavrantonis15-Oct-13 5:11 
Questionconnect to mdf sql database Pin
Mahdi Nejadsahebi20-Dec-12 1:26
MemberMahdi Nejadsahebi20-Dec-12 1:26 
QuestionUsing the CDatabase class to read an Access databases-->write Pin
Member 942749014-Sep-12 22:13
MemberMember 942749014-Sep-12 22:13 
GeneralMy vote of 5 Pin
Mahdi Nejadsahebi27-Apr-12 20:15
MemberMahdi Nejadsahebi27-Apr-12 20:15 
GeneralError messages in running Using the CDatabase class to read an Access databases Pin
shiphy21-Mar-10 21:03
Membershiphy21-Mar-10 21:03 
GeneralRe: Too much in Try/Catch [modified] Pin
shiphy19-Mar-10 12:15
Membershiphy19-Mar-10 12:15 
GeneralToo much in Try/Catch Pin
William Winner19-Mar-10 10:49
MemberWilliam Winner19-Mar-10 10:49 
GeneralHaving problems showing data in my Access Database in the List Control Pin
shiphy19-Mar-10 10:18
Membershiphy19-Mar-10 10:18 
GeneralDatabase Error [modified] Pin
S p k 52129-Jun-08 20:24
MemberS p k 52129-Jun-08 20:24 
GeneralRe: Database Error Pin
zhaque3-Feb-10 15:00
Memberzhaque3-Feb-10 15:00 
GeneralDataBase Error Pin
S p k 52129-Jun-08 20:22
MemberS p k 52129-Jun-08 20:22 
GeneralThank you very much Pin
Jellow TK27-Feb-08 9:49
MemberJellow TK27-Feb-08 9:49 
QuestionHow to use Other databases Like "SQL"" Pin
rag84dec21-May-07 21:27
Memberrag84dec21-May-07 21:27 
QuestionRe: Multiple table help, please? Pin
mla15427-Feb-07 7:08
Membermla15427-Feb-07 7:08 
QuestionAccess file creation Pin
Arun M S17-Jan-07 19:28
MemberArun M S17-Jan-07 19:28 
QuestionIn modal dglboxes? Pin
swapna_signsin11-May-06 1:22
Memberswapna_signsin11-May-06 1:22 
Generalc/c++ with sql/access database Pin
Anonymous5-Oct-05 23:35
MemberAnonymous5-Oct-05 23:35 
Generalunresolved external symbol Pin
WinAPILearner31-Oct-04 12:44
MemberWinAPILearner31-Oct-04 12:44 
GeneralRe: unresolved external symbol Pin
Anonymous18-Feb-05 11:46
MemberAnonymous18-Feb-05 11:46 
GeneralRe: unresolved external symbol Pin
zhaque31-Oct-05 8:25
Memberzhaque31-Oct-05 8:25 
GeneralBound error Pin
vivadot18-Oct-04 12:24
Membervivadot18-Oct-04 12:24 
AnswerRe: Bound error Pin
liuxiaop12-Feb-07 3:43
Memberliuxiaop12-Feb-07 3:43 
Generaldatabase + listcontrol Pin
sakesh16-Sep-04 21:50
Membersakesh16-Sep-04 21:50 
QuestionUnicode? Pin
Badaa25-Apr-04 3:19
MemberBadaa25-Apr-04 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.