I am trying to add access to a database (currently extracted into MS Access 97 during development) into an existing MFC application that did not originally include database support. I have registered the database as "RAMS" through the ODBC utility on the Windows Control Pnnel. I used ClassWizard to extend the CRecordset class as CCuHdrSet and configured within the wizard to my RAMS database and one table within it CuHdr. I included <afxdb.h> in the CuHdrSet.h and my application document's .cpp file, Guy3Doc.cpp. I have created the following function to retrieve a record set which compiles fine but crashes at runtime. I get a message box on my screen when I attempt to open the CRecordset derived object (CCuHdrSet) that displays the message "failed to create empty document". When the OK button on the message dialog is clicked, the application exits. If I comment out the hdrSet.Open(....) statement, the application does not terminate and the message dialog does not appear. Clearly I am doing something wrong here but I have not been able to determine my error. I need to use the recordset to obtain results of the query but haven't gotten there yet as I can't get past this error. The code snippet is provided below. If you can help, I would greatly appreciate it.
void CGuy3Doc:oQuery(void){
CDatabase m_Database;
if(!m_Database.Open(NULL, FALSE, TRUE, "ODBC;DSN=RAMS"){
AfxMessageBox("Failed to open the database connection."
}
CCuHdrSet hdrSet(&m_Database);
hdrSet.m_strFilter = "";
// the following statement is all on a single line
// in my actual program.
if(!hdrSet.Open(AFX_DB_USE_DEFAULT_TYPE, "SELECT [TITLE] FROM [TIDCUHDR] WHERE [COMPATIBLE_UNIT] = 'S0440'" || hdrSet.IsEOF())
{
AfxMessageBox("Cannot find records."
}
// add in use of recordset here
hdrSet.Close();
m_Database.Close();
}
void CGuy3Doc:oQuery(void){
CDatabase m_Database;
if(!m_Database.Open(NULL, FALSE, TRUE, "ODBC;DSN=RAMS"){
AfxMessageBox("Failed to open the database connection."
}
CCuHdrSet hdrSet(&m_Database);
hdrSet.m_strFilter = "";
// the following statement is all on a single line
// in my actual program.
if(!hdrSet.Open(AFX_DB_USE_DEFAULT_TYPE, "SELECT [TITLE] FROM [TIDCUHDR] WHERE [COMPATIBLE_UNIT] = 'S0440'" || hdrSet.IsEOF())
{
AfxMessageBox("Cannot find records."
}
// add in use of recordset here
hdrSet.Close();
m_Database.Close();
}