0

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.')
3
  • You might wanna have a look at this thread; How to skip Autoexec macro when opening MSAccess from MSAccess?
    – Rene
    Commented Dec 3, 2018 at 20:39
  • The problem with that is that the bypass keys are disabled. Pressing them via API would probably be the same as holding them when double clicking - thus it's prevented in the general state these DBs exist in when in production. The way the DB is designed I can't interrupt the AutoExec macro without first opening up the DB and using this security form to unlock it (by design). Commented Dec 3, 2018 at 20:43
  • NOTE: With Erik's answer below Rene's comment becomes a viable way to bypass the AutoExec macro. Thanks everyone! Commented Dec 3, 2018 at 21:07

1 Answer 1

1

You can toggle the bypass key before opening the database.

objDBE = win32com.client.Dispatch(r'DAO.DBEngine')
objDB = ojbDBE.OpenDatabase(db)
objDB.Properties[r'AllowBypassKey'] = false
objDB.Close

Then, you can open it while holding shift as shared by Rene, and then turn the bypass key back on.

Opening it through DAO doesn't trigger macros or VBA code.

Do note that if your only objective is to iterate through the objects, I recommend only using DAO and querying MSysObjects instead of automating the Access Application object. That avoids any issue with running VBA code or AutoExec macro's.

3
  • That will do it - thank you. I wasn't aware that you could trigger the bypass keys via DAO without opening the DB. Can you also execute a function with DAO without opening the physical database? We have an enable and disable function for the various security features in the DB so it would be easy to just call those if possible. The goal wasn't only to iterate through the objects. I was simply using that code for testing to ensure the Access.Application object was working as intended. Commented Dec 3, 2018 at 21:05
  • @PythonAutomationDev Nope, through DAO you can only access database properties, tables and queries, and you can only execute queries that don't contain any user-defined functions or reserved functions (if you think you can be clever because Run or Eval is a built-in function that won't work). You can modify a lot of things through database properties, though, such as the startup key, startup forms, some security settings.
    – Erik A
    Commented Dec 3, 2018 at 21:11
  • Thanks for the heads up. I haven't done much DAO programming (ODBC and ADO take care of most of my needs). I appreciate your help. Commented Dec 3, 2018 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.