Connecting to MS Access 2007 (.accdb) database using pyodbc

havelock picture havelock · Jun 18, 2011 · Viewed 17.3k times · Source

I am on Win7 x64, using Python 2.7.1 x64. I am porting an application I created in VC++ to Python for educational purpouses.
The original application has no problem connecting to the MS Access 2007 format DB file by using the following connection string:
OleDbConnection^ conn = gcnew OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|DB.accdb");
Now, when I try to connect to the same DB file (put in C:\ this time) in Python using pyodbc and the following conenction string:
conn = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\DB.accdb;")
, and no matter whether I keep the OLEDB provider or I use the Provider=MSDASQL; as mentioned here (MS mentions it's not availiable for 64bit), I keep getting the following error:

pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')

What might cause this problem?

ADD: I have looked into pyodbc docs more closely and tried conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c:\\DB.accdb;") - the same error. This is really weird, since the pyodbc.dataSources() shows that I have this provider.

ADD2: I tried win32com.client usage such as here in order to connect by using OLE DB - no success. Seems that it's impossible, nothing works.

Answer

Renaud Bompuis picture Renaud Bompuis · Jun 19, 2011
  1. Try to use something like the following instead of using the same string as the one for OLeDb:
    "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\DB.accdb;"

  2. You may not be able to talk to the driver directly from your x64 Python application: Access 2007 and its ACE driver are 32 bits only.
    Instead, get the ACE x64 driver for Access 2010, but be careful that if you already have Access or the ACE driver 32bit installed, it won't work.
    I would stick to the 32bit versions of Python and of the ACE driver if you expect your app to be run on other systems: it is not recommended to mix x64 and x86 versions of Office tools and drivers, you'll probably end up with lots of issues if you do.

  3. If the issue is not with the 32/64bit mix, then maybe this question has the answer you seek.