Using DAO to read data sources other than MS Access | CodeGuru

Using DAO to read data sources other than MS Access

MS Access (.mdb) is not the only data source you can use as a DAO database. Many other types can be opened directly without ODBC. The secret lies in the parameters passed to CDaoDatabase::Open(). If you’re opening an Access file, you can omit all but the first parameter: CDaoDatabase db; db.Open(“c:\test.mdb”); But there are three […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

MS Access (.mdb) is not the only data source you can use as a DAO database. Many other types can be opened directly without ODBC.

The secret lies in the parameters passed to CDaoDatabase::Open(). If you’re opening an Access file, you can omit all but the first parameter:

CDaoDatabase db;
db.Open("c:\test.mdb");

But there are three more parameters for Open(). Here’s the prototype:

Open( LPCTSTR lpszName,
      BOOL bExclusive=FALSE,
      BOOL bReadOnly=FALSE,
      LPCTSTR lpszConnect="");

If lpszConnect is blank (the default value), it designates you’re opening an Access file. The following table shows the connect string used to open all the types supported by DAO:

File Type Connect String
Access blank
dBASE III dBASE III;
dBASE IV dBASE IV;
dBASE 5 dBASE 5.0;
Paradox 3.x Paradox 3.x;
Paradox 4.x Paradox 4.x;
Paradox 5.x Paradox 5.x;
FoxPro 2.0 FoxPro 2.0;
FoxPro 2.5 FoxPro 2.5;
FoxPro 2.6 FoxPro 2.6;
Excel 3.0 Excel 3.0;
Excel 4.0 Excel 4.0;
Excel 5.0 or Excel 95 Excel 5.0;
Excel 97 Excel 97;
HTML Import HTML Import;
HTML Export HTML Export;
Text Text;

Opening an Excel spreadsheet in an MFC application

CDaoDatabase* pDB = new CDaoDatabase;
pDB->Open("c:\test.xls",  //path + file name
     FALSE,           //exclusive?
     FALSE,           //read only?
     "Excel 5.0;");   //connect string
CDaoRecordSet rs(pDB);
rs.Open(dbOpenDynaset, "select * from [Sheet1$]");

Excel is handled a little differently than Access. DAO treats any named range in the sheet as a table, and each column in the range becomes a column in the table. The first row in the range is not included as a row in the table; instead, it is used to assign the column names. DAO also treats the entire sheet as a table, using the sheet name as the table name (as in the example above) and using the first row in the sheet to assign column names. A couple of things to note about using the sheet name as the table name: You must suffix it with a $ and enclose it in square brackets.

Opening a dBase, FoxPro or Paradox database

These databases differ from Access in that one file only contains one table. Opening one of these is similar to opening an Access database, except you only supply the path to the database as the first parameter to Open(), and you use the file name as the table name in your SQL. Here’s an example of opening a FoxPro database:

CDaoDatabase* pDB = new CDaoDatabase;
pDB->Open("d:\source\bosold\db",  //path only!
     TRUE,           //exclusive?
     TRUE,           //read only?
     "FoxPro 2.6;");   //connect string
CDaoRecordset rs(pDB);
rs.Open(dbOpenDynaset, "select CUST from [customer.dbf]");

Here’s how you can modify the MFC sample app “DAOTable” to support multiple types.

1) add a selection “all files (*.*)” to the file dialog in CDAOTableDlg::OnButtonConnect():

CFileDialog dlg (TRUE,_T("mdb"),NULL,OFN_HIDEREADONLY,
   _T("Access Files (*.mdb)|*.mdb|all files (*.*)|*.*||"),this);

2) After calling DoModal(), get the extension of the selected file:

CString strDatabaseExt = dlg.GetFileExt();

3) Add a parameter to openDatabase() to receive the file extension:

[the declaration]
int openDatabase(CDaoDatabase **ppDatabase,
                 CString fileName,
                 BOOL bReportNoOpen = TRUE,
                 CString fileExt = "");

[the definition]
int openDatabase(CDaoDatabase **ppDatabase,
                 CString fileName,
                 BOOL bReportNoOpen,
                 CString fileExt)

4) Meanwhile back in OnButtonConnect(), pass the file extension to openDatabase():

// now open the database if possible
int retCode = openDatabase(&m_pDatabase,
              m_strDatabaseName,
              TRUE,
              strDatabaseExt);

5) Test the file extension in openDatabase() and set the connect string accordingly:

if (ppDatabase == NULL)
   return -1; // fatal error

fileExt.MakeLower();
CString strConnect;

if (fileExt == "xls")
   strConnect="Excel 5.0;";
else if (fileExt == "mdb")
   strConnect="";
else
   {
      AfxMessageBox("File type not supported");
      return -1;
   }

// now open the database object with error checking
try
{
   (*ppDatabase)->Open(fileName,
                       FALSE,
                       FALSE,
                       strConnect);
}
catch (CDaoException *e)
{
   etc.

You can use this version of DAOTable to explore the structure of a file as seen by DAO.


CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.