Search code examples
pythonms-accessgocom

Web server MSAccess error: You canceled the previous operation


I am getting a cryptic MSAccess error and I can't figure out what is causing it.

I have the following setup:

  • Golang / Gin webserver (Windows) that calls a
  • compiled python script (.exe) that uses COM to call a
  • MSAccess macro to export a database query to a certain format.

I also have some primitive logging on multiple layers to help me debug in such a weird architecture.

The error is logged from python: <class 'pywintypes.com_error'>: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, None, 'Sie haben den vorherigen Vorgang abgebrochen.', 'vbaac10.chm', 5738, -2146826287), None)

which roughly translates to

<class 'pywintypes.com_error'>: (-2147352567, 'Unknown error.', (0, None, 'You canceled the previous operation.', 'vbaac10.chm', 5738, -2146826287), None).

The strange thing is that if I run the python executable by myself, everything works as expected and no errors occur. If I call it from my golang code (webserver / tests) however, it throws the 'You canceled the previous operation.' error, which as far as I can tell from reading online occurs when the macro name or table name is incorrect.

The only hypothesis I have right now is that it is because of different user rights when my golang executes it as to when I execute it myself?

Here is the relevant python code used to call the macro:

import pythoncom
import win32com.client
import sys
import logger

log = logger.Logger("data-sync.txt")

def export_db(db_path):
    try:        
        log.info(f"Exporting database under: {db_path}")

        log.info("- Connecting to Access COM interface.")
        oAccess = win32com.client.Dispatch("Access.Application")

        log.info("- Opening provided database.")
        oAccess.OpenCurrentDatabase(db_path, False)

        log.info("- Running Macro \"ExportDb\"")    
        oAccess.DoCmd.RunMacro("ExportDb")

        log.info("- Running complete, closing database")        
        oAccess.CloseCurrentDatabase()
    except Exception as err:        
        log.error(type(err), err)
        pass
    pass

I have triple-checked the paths and the macro name being called, any other ideas are welcome.


Solution

  • I figured out at least why it was partially not working. Apparently in my executable I had a hard-coded default path like the following:

    default_db_path = r"C:\Users\some\path\DummyData.accdb"
    

    However I was providing a path of the following format:

    export-database.exe "C:/Users/some/path/DummyData.accdb"
    

    The different slashes were apparently enough for Access or the COM interface to interpret it as a relative path and crash.

    Unfortunately my test that does exactly the same thing still throws the same error for corrected paths. My productive code works, so I will mark this question as answered and if I come across the cause of my test problem I will update this.

    Edit: As advertised, I came across the issue that was occurring with my tests. In my tests I was copying the access database to a subfolder to work on it, and clean it up after my tests.

    Apparently you have to activate the execution of macros in the Access trust center (this is a global setting). I had activated the macro in the source database, but after the automated copying in my tests I was apparently getting a notification in the database itself when I opened the actual db file and tried to manually execute the macro!