Search code examples
python-3.xms-accesscomtypes

Python MS Access COM "Server execution failed"


I recently had to reinstall Office on my computer. I am running Windows 7 home premium x64, and had to install Office 2016 x64 (click-to-run). I initially had some problems due to my Path environmental variable (not sure when THAT got messed up).

I also tried to install the ODBC drivers for Access and eventually resorted to installing the Access 2013 x64 Runtime Environment to get it. The 2016 Database Engine x64 and 2016 Runtime Environment x64 both did not want to install on my computer, probably because I had click-to-run Office 2016 installed.

Now I'm having trouble using COM commands in Python (x64, 3.5.2, Canopy environment). The specific error I get is when it tries to create an Access.Application object, and the error is "[WinError -2146959355] Server execution failed"

I suspect the issue might be due to the multiple versions of Access now available, as I currently have two "Open with" options for an Access database. The first appears to be "Office15" Access, from the Runtime Environment installer. The later is "Office16" Access, which is fully featured.

I suspect that either by editing my registry or the "Access.Application" string I could get this to work. Any ideas?

Here's the minimum code I am working with

from comtypes.client import CreateObject

access = CreateObject('Access.Application') #Error occurs here

from comtypes.gen import Access

DBEngine = access.DBEngine
db = DBEngine.CreateDatabase('test234.mdb', Access.DB_LANG_GENERAL)
db.Close()

Solution

  • You can't use com to create new Access.Application instances based on the runtime variant of Access. Using the runtime environment and the normal environment together on one computer is not supported, afaik. You can use the /runtime switch if you need to test things in runtime mode.

    However, if you're looking to work with the DAO database engine, you can do that:

    DBEngine = CreateObject('DAO.DBEngine.120')
    db = DBEngine.CreateDatabase('test234.mdb', Access.DB_LANG_GENERAL)
    db.Close()