0

I'm trying to execute a function in vba inside a ms access db with python.

I tried this:

strDbName = "PathToMyDb"
ac = win32com.client.Dispatch ( "Access.Application" )
ac.OpenCurrentDatabase ( strDbName,False)
ac.Run("Performance_Attr_ModuloXX")
ac.Application.CloseCurrentDatabase ( )
ac.Application.Quit ( )

But I keep getting this error:

Traceback (most recent call last):
  File "C:\Users\PIN\PycharmProjects\test_db\main.py", line 32, in <module>
    ac.Run("Performance_Attr_ModuloXX")
  File "<COMObject Access.Application>", line 14, in Run
  File "C:\Users\PIN\PycharmProjects\test_db\venv\lib\site-packages\win32com\client\dynamic.py", line 369, in _ApplyTypes_
    *(dispid, LCID, wFlags, retType, argTypes) + args
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352562), None)

I already tried to check the function in vba and it works.

freeflow
  • 4,129
  • 3
  • 10
  • 18
Rip
  • 3
  • 1
  • 2
  • No, it does it with every function or subroutine I try – Rip Feb 24 '23 at 17:56
  • Does this answer your question? [How to run a MS Access macro from python](https://stackoverflow.com/questions/9177984/how-to-run-a-ms-access-macro-from-python) – June7 Feb 24 '23 at 19:14
  • Nope, I need to run a function not a Macro, I found how to print the reason of the issue with: print ( win32api.FormatMessage ( -2147352562 ) ) And it prints out the following: Invalid number of parameters. I don't understand since my function doesn't require any parameter, I also tried to set 1 parameter but it gives the same error code – Rip Feb 24 '23 at 19:16
  • It shows example code for both VBA and macro. It also shows code to Import library, which you don't show. – June7 Feb 24 '23 at 20:57
  • If the problem was the import or else the printed error would have been different, beside I already specified the problem is not vba. – Rip Feb 24 '23 at 21:06
  • I have no problem calling a Sub from a script but calling a Function is problematic if Function is designed to return a value. Unfortunately, don't know Python well enough to advise further. – June7 Feb 24 '23 at 21:26

0 Answers0