I'm looking for a way in Python to either close a form without executing the code in Form_Unload or a way to hit OK on a MsgBox thrown using Access VBA.
I am attempting to do some automation and scripting against a large number of Access DBs using Python, however I've run across a problem with our specific Access setup. Our databases (like most client facing Access applications) have AllowSpecialKeys and AllowBypassKeys turned off. However, if you are a member of our Admin group a form loads using the AutoExec macro when you enter the database. When this form closes it asks if you would like to turn these properties back on or not using a VB Yes/No MsgBox. This allows us to unlock the databases and perform maintenance, then re-lock them for deployment.
I'm able to enter the database using Python, scrape all the information I need, and then back out of the database. The problem comes in that when I exit the database using Python, on the CloseCurrentDatabase call the form unloads and the pop-up box comes up. I then can't handle the MsgBox. Does anyone know of either a way to close the MsgBox, or to close the form without executing the form unload event? If not, we could modify the security form to have lock and unlock buttons on the form and not prompt on close, however we would have to push the changes out to a large number of databases so this would be difficult.
To replicate the environment I'm working in, create a form in an Access database. Under the Form_Unload event add a MsgBox call in VBA. Then, create an Access macro called AutoExec and have it open the form. This will cause the form to open when the database is opened and when the DB is closed, this form will close causing the popup.
Below is my Python code. Thanks in advance for any assistance.
#libraries
import win32com.client
#set the DB
db = r'FILE PATH REMOVED - REPLACE THIS STRING WITH FILE PATH'
#use Access com object
objAccess = win32com.client.Dispatch(r'Access.Application')
#open Access DB
objAccess.OpenCurrentDatabase(db, False)
#error handler
try:
#loop through forms and print them
for form in objAccess.CurrentProject.AllForms:
print(form.name)
del form
#loop through reports and print them
for report in objAccess.CurrentProject.AllReports:
print(report.name)
del report
#loop through macros and print them
for macro in objAccess.CurrentProject.AllMacros:
print(macro.name)
del macro
#loop through modules and print them
for module in objAccess.CurrentProject.AllModules:
print(module.name)
del module
#cleanup
finally:
#close database and access
objAccess.CloseCurrentDatabase
objAccess.Quit(2)
del objAccess
#note cleanup completion for testing
print('Cleanup completed.')
#note code completion for testing
print('All done.')