MFC ODBC Excel Driver -- Exception Thrown Roaming::RoamingNotInitializedException

Andre LeBlanc 21 Reputation points
2022-03-31T11:20:18.217+00:00

Hello,

I have a connection to an Excel file via ODBC. Everything works as expected until I shut down the application and get this exception (multiple times) in debug:

Exception thrown at 0x00007FFECD2F478C in MHM.exe: Microsoft C++ exception: Roaming::RoamingNotInitializedException at memory location 0x000000C68E74F690.
Exception thrown at 0x00007FFECD2F478C in MHM.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.

void CMHMView::UpdateAMS2140Database(CString filename)
{
    CString strConnect;// = m_pExcelConfigTable->GetDefaultConnect();
    CString sDriver;
    CString sSql;

    sDriver = GetExcelDriver();
    if (sDriver.IsEmpty())
    {
        // Blast! We didn´t find that driver!
        AfxMessageBox(L"No Excel ODBC driver found");
        return;
    }

    strConnect.Format(L"ODBC;DBQ=%s;DRIVER={%s}", filename, sDriver);
//  strConnect = m_pExcelConfigTable->GetDefaultConnect();
    TRY
    {
        m_exceldatabase.Open(NULL, FALSE, FALSE, strConnect, FALSE);

        sSql = L"SELECT * "
            L"FROM AMS2140CONFIGTABLE ";

        CAMS2140ConfigTable exceltable(&m_exceldatabase);

        exceltable.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);

    //  exceltable.Open();
        CAMS2140Part part(&m_database);
        part.Open();

        while (!exceltable.IsEOF())
        {
            if (exceltable.m_Sel == part.m_Sel)
            {
                part.Edit();
                part.m_UnitList = exceltable.m_ListPrice;

                part.Update();
                part.MoveNext();
            }
            exceltable.MoveNext();
        }

        exceltable.Close();
        part.Close();

        m_database.Close();
        m_exceldatabase.Close();
    }
    CATCH(CDBException, e)
    {
        // A database exception occured. Pop out the details...
        AfxMessageBox(L"Database error: " + e->m_strError);
    }
    END_CATCH;
}


// Query an Excel file
void CMHMView::QueryExcel(CString filename)
{
    CDatabase database;
    CString sSql;
    CString sItem1, sItem2;
    CString sDriver;
    CString sDsn;
    CString sFile = filename; 

    sDriver = GetExcelDriver();
    if (sDriver.IsEmpty())
    {       
        AfxMessageBox(L"No Excel ODBC driver found");
        return;
    }

    // Create a pseudo DSN including the name of the Driver and the Excel file
    // so we don´t have to have an explicit DSN installed in our ODBC admin
    sDsn.Format(L"ODBC;DRIVER={%s};DBQ=%s;DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;", sDriver, sFile);

    TRY
    {
        // Open the database using the former created pseudo DSN
        database.Open(L"", false, false, sDsn);

    // Allocate the recordset
    CRecordset recset(&database);

    // Build the SQL string
    // Remember to name a section of data in the Excel sheet using
    // "Insert->Names" to be able to work with the data like you would
    // with a table in a "real" database. There may be more than one table
    // contained in a worksheet.
    sSql = L"SELECT * "
        L"FROM AMS2140CONFIGTABLE ";
         //  L"ORDER BY PartNo";

    // Execute that query (implicitly by opening the recordset)
//   recset.Open();
    recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);

    CAMS2140Part part(&m_database);
    part.Open();
    int nrow = 1;

    // Browse the result
    while (!recset.IsEOF())
    {
        // Read the result line
        recset.GetFieldValue(L"Sel", sItem1);
        recset.GetFieldValue(L"ListPrice", sItem2);
        if (sItem1 != L"")
        {
            if (part.m_Sel == sItem1)
            {
                part.Edit();
                part.m_UnitList = _ttof(sItem2);


                part.Update();
                part.MoveNext();
            }
        }


        //Update the rows and check for errors
        //int nRetCode;
        //AFX_ODBC_CALL(::SQLSetPos(part.m_hstmt, NULL, SQL_UPDATE, SQL_LOCK_NO_CHANGE));
        //part.CheckRowsetError(nRetCode);



        // Skip to the next resultline
        recset.MoveNext();
    }

    part.Close();
    recset.Close();
    // Close the database
    database.Close();

    }
        CATCH(CDBException, e)
    {
        // A database exception occured. Pop out the details...
        AfxMessageBox(L"Database error: " + e->m_strError);
    }
    END_CATCH;
}

May you please help?
Thank you!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,916 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Gary Gray 106 Reputation points
    2022-07-21T20:41:20.807+00:00

    mine too.... If I install the older access driver "accessdatabaseengine_X64.exe" then it works fine. Seems Microsoft broke it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.