简体   繁体   中英

Why doesn't my VBA function shut down Access properly?

This question is related to my previous question found here: Python code for running ms-Access Module Subroutine

To explain my problem I will give a breif description of what I am trying to do:

I have made a GUI using python and tkinter that lets the user choose different search criterias, and then prints these criterias to a .txt file. The python code then launches an instance of Access using the following code:

objAccess = win32com.client.Dispatch("Access.application")
objAccess.Visible = True
objAccess.OpenCurrentDatabase(filepath)
objAccess.DoCmd.RunMacro("Macro1")

The macro is a simple "RunCode" command with Function Name My_Function(). My_Function() reads the .txt file, performs queries to the database, generates reports and saves the reports in pdf-format in the current folder and then shuts down Access using:

DoCmd.Quit

I am pretty sure this all worked as intended, until I moved the files (the python script and the Access database) to a new folder. After the move, everything runs just as before, ie Access opens a window, generates the reports according to the user input and then shuts down. BUT unlike before I moved the files, the python console (I'm using Spyder with Python 2.7) reports the following exception:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:/Users/nx71/ProsjektreferanserPython/Test_final/GUI_02_norsk.py",
line 269, in buttonclick2
    objAccess.DoCmd.RunMacro("Macro1")
  File "<COMObject <unknown>>", line 3, in RunMacro
com_error: (-2147352567, 'Exception occurred.', (0, None, u'The | action was canceled.
', None, -1, -2146825787), None)

Additionally, after running the script, I am unable to open any Access database by doubleclicking the icon, unless I enter the Task Manager, select the single Access background process and "End Task".

So in short: The script+macro+function does what I want it to do, but after I moved the files, the console reports an exception and an Access bacground process lingers and prevent me from opening a new instance of Access.

Ps: I'm using Windows 10 and MS Office 2013.

Any help will be greatly appreciated!

Thank you, DanL, for your comment. It sent me on the right track. I think the problem was caused by the VBA function shutting down Access with the DoCmd.Quit while objAccess was still "active" in the python script (forgive my lack of proper terminology, as I am very new to programming).

Removing DoCmd.Quit from the end of the VBA function and adding objAccess.Quit() after the RunMacro in the python code instead gave me the desired result.

I am beginning to doubt that it all worked properly before I moved the files.

Thanks again, DanL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM