How do I execute code before any form opens?

M

Marc Hillman

I need to execute a piece of code before any form opens. I've got lots of
subforms (which execute before the main form), but I'm not clear which opens
first. How can I run some initialisation code before any form is Opened?

Delete the word "REMOVE" from my address to reply direct
_______________________________________
Marc Hillman, Melbourne, Australia
web: http://users.tpg.com.au/mhillman/
 
M

MikeB

1. Add A Module to your project
2. In the module, create a Public function called Startup and in the function, place whatever code you
want to execute first, ending with the DoCmd.OpenForm WhateverForm is your startup form.
3. Create a Macro called Autoexec.
4. In the Macro Autoexec, select "RunCode" as the Action.
5. For The Function Name, click on the elipsis to bring up the fucntion selection dialog.
6. Open the +Functions in the lower part of the dialog and click on the value that is the name of your
database.
7. In the middle listbox, select the name of the module you added.
8. in the right listbox, double click the name of the function "Startup" and click the "OK" to close the
dialog.
10. Close and save the Macro.

The Macro Autoexec will execute on startup and call the function "Startup".
 
M

Marc Hillman

Thanks for the suggestion, but as I'm doing development a lot, I enter
Access with the shift key down, which I believe bypasses AutoExec. Are there
solutions not involving AutoExec?

MikeB said:
1. Add A Module to your project
2. In the module, create a Public function called Startup and in the
function, place whatever code you
want to execute first, ending with the DoCmd.OpenForm WhateverForm is your startup form.
3. Create a Macro called Autoexec.
4. In the Macro Autoexec, select "RunCode" as the Action.
5. For The Function Name, click on the elipsis to bring up the fucntion selection dialog.
6. Open the +Functions in the lower part of the dialog and click on the value that is the name of your
database.
7. In the middle listbox, select the name of the module you added.
8. in the right listbox, double click the name of the function "Startup"
and click the "OK" to close the
 
M

MikeB

Marc Hillman said:
Thanks for the suggestion, but as I'm doing development a lot, I enter
Access with the shift key down, which I believe bypasses AutoExec. Are there
solutions not involving AutoExec?

I believe the shift key bypasses both startup methods, the default form and autoexec. I guess you could
write a vbscript to automate access and execute some stuff that way and then start Access from the script
sans automation.
 
D

Dirk Goldgar

Marc Hillman said:
I need to execute a piece of code before any form opens. I've got
lots of subforms (which execute before the main form), but I'm not
clear which opens first. How can I run some initialisation code
before any form is Opened?

Subforms open before their parent forms, for what that's worth.
Normally I'd say to use a an autoexec macro to run your startup code,
but I see you've nixed that.

Does this code have to execute before any form is actually opened, or
only before a form is actually displayed? I could conceive of having an
initialization routine in a standard module that keeps track of whether
it has already been executed (using a static variable) and simply exits
if it has; e.g.,

Function DBStartup()

Static blnAlreadyRun As Boolean

If blnAlreadyRun Then
Exit Function
End If

blnAlreadyRun = True

' ... rest of startup code goes here ...

End Function


Then you'd have *every* form call this function from its Open event.
 
D

Dirk Goldgar

Marc Hillman said:
That's exactly the solution I came up with - it just seems clumsy.

The only other suggestion I could make is to have the function called
from the autoexec macro, as previously mentioned, but whenever you open
the database while holding down the shift key you must remember to run
the function manually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top