Python code for running ms-Access Module Subroutine

后端 未结 2 1241
Happy的楠姐
Happy的楠姐 2020-12-10 19:58

I am very new to programming and this is my first question on stackoverflow. I am trying to make python open an .accdb file and run a subroutine which is already defined in

2条回答
  •  遥遥无期
    2020-12-10 20:11

    Consider creating a new Access macro object with a RunCode action that calls the function in the module. Then, call the macro in Python's Windows COM API using use the DoCmd.RunMacro method.

    MACRO

    Macro
    RunCode: TestMe()
    

    NOTE: Only functions can be referenced with RunCode not subroutines unless you create a VBA module function that calls the subroutine: Call SubroutineName:

    Python

    import win32com.client
    ac = win32com.client.Dispatch("Access.Application")
    ac.Visible=True
    ac.OpenCurrentDatabase("\\testdb.accdb")
    ac.DoCmd.RunMacro('MacroName')
    
    ac.DoCmd.CloseDatabase
    ac = None
    

提交回复
热议问题