5

I have an auto exec script in access 2010 that executes as soon as the DB is loaded up by a user. The problem is, until that user enables macros, the script fails and gives an error because it's not allowed to run yet.

Is there a way to delay the autoexec until "enable macros" is selected, and then trigger it?

Or is there a way to trigger code without an autoexec, that is tied to the "enable macros" selection?

2
  • Autoexec doesn't run until Enable macros is clicked does it?
    – aphoria
    Commented May 1, 2014 at 13:17
  • It tries to run right away, as soon as the DB is opened, but because Enable Macros isn't yet clicked it kicks back an error. Commented May 1, 2014 at 13:26

1 Answer 1

6

I found How to force user to deal with the Security Warning when starting Access 2007?, which led me to this Microsoft support article.

From the Microsoft article:

You can use an AutoExec macro to test whether a database is trusted when you open the database. Additionally, the macro can open a form that displays a customized message to users if the database is not trusted. This message lets users know that the database must be enabled or trusted for the code to run successfully.

To create the AutoExec macro and the form, follow these steps:

  1. Create a new form in Design view.
  2. Add a text box or a label to the form, and then type the information that you want to display to the user.
  3. Save and then close the form.
  4. Create a macro, and then name the macro AutoExec.
  5. Show the Conditions column.
  6. Type the following line in the Conditions column:

    CurrentProject.IsTrusted = False

  7. In the Actions column, click OpenForm.
  8. In the Form Name box under Action Arguments, click the form that you created in step 1.
  9. Save and then close the macro.

When the database opens, the AutoExec macro starts and then tests the IsTrusted condition. If the database is not trusted by Access 2007, the macro opens the form that you specified in the "OpenForm" action of the macro.

The macro editor is different in 2010, but I was able to create an Autoexec macro that checked CurrentProject.IsTrusted = False and then displayed my form. You can put a message on your form telling the user to enable macros.

3
  • 2
    This worked for me, it actually works out better than what I was planning. Thanks! Commented May 1, 2014 at 15:51
  • 1
    This works in Access 2013 also - make your form a regular form, not a dialog, with a label that says something like "Click the Enable Content button above to allow Access to run the code in this database". Then add an Else clause to the If statement in the macro and call your regular startup routine there - then when the user clicks the Enable Content button, the database will be reopened and that routine will be run. Commented Jul 29, 2015 at 22:38
  • Perfect Solution! Together with a custom Ribbon and other Startup procedures i was able to produce a completely locked down DB.
    – rohrl77
    Commented Aug 25, 2020 at 12:45

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.