Search code examples
c++ms-accessvisual-c++ado

Unrecognised database format with ACCESS and ADO


ALL,

I have a MS Access accdb file, to which I try to connect:

    std::wstring dbFile = pimpl->m_connectString.substr( pimpl->m_connectString.find( L"DBQ=") + 4 );
    dbFile = dbFile.substr( 0, dbFile.find( L";" ) );
    if( FAILED( CoInitialize( NULL ) ) )
    {
        errorMsg.push_back( L"Failed to initialize DAO interface" );
        result = 1;
    }
    if( !result )
    {
        LPCSTR version;
        std::wstring connectString = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        connectString += dbFile;
        _bstr_t strCnn( connectString.c_str() );
        _ConnectionPtr pConnection = NULL;
        try
        {
            if( FAILED( pConnection.CreateInstance( __uuidof( Connection ) ) ) )
            {
                errorMsg.push_back( L"Failed to create a connection to ACCESS engine" );
                result = 1;
            }
            else
            {
                pConnection->Open( strCnn, "", "", adConnectUnspecified );
                version = (LPCSTR) (_bstr_t) pConnection->Properties->GetItem("DBMS Version")->Value;
            }
        }
        catch( _com_error &e )
        {
            ErrorPtr pErr = NULL;
            if( ( pConnection->Errors->Count ) > 0 )
            {
                long nCount = pConnection->Errors->Count;
                // Collection ranges from 0 to nCount -1.
                for( long i = 0 ; i < nCount ; i++ )
                {
                    std::wstring error = L"Error is ";
                    pErr = pConnection->Errors->GetItem( i );
                    std::string descr( ((LPCSTR) pErr->Description) );
                    printf( "Error number: %x\t%s\n", pErr->Number, (LPCSTR) pErr->Description );
                }
            }
        }
        CoUninitialize();

However upon execution I'm getting aforementioned error.

What am I missing?

I am working with MSVC 2017 Community on Windows 8.1.

I have no idea what engine the file was created with.

TIA!


Solution

  • You are probably using the wrong provider. For accdb files use the following:

    Provider=Microsoft.ACE.OLEDB.12.0;Data 
    Sourcee=C:\myFolder\myAccessFile.accdb;Persist 
    Security Info=False;
    

    You may have to install the required ACE drivers freely available from the M$ site.