In a previous post I tossed out a concept for a global form handler that could be used to generalize form behavior without having to add any code to forms. After having played with it a few days and hashed a few things around, here’s a refined version of actual drop-in code that you can use which seems to cover most cases that I’ve run into.

Aside from generic behavior, this also handles multiple form instances (use the UI.OpenMultiInstanceForm() procedure for that).

Two more caveats other than the HasModule thing mentioned in the original post:

1) acDialog

Because the handler can’t register the form internally until after the form is instanced, for dialog form’s it’s pretty much inevitable that for dialog forms, the initialization needs to be called from within the form instead of the full-auto handling. This is provided for, just call UI.PreRegisterForm() from within any acDialog opened forms.

2) Native Open Events Fire First

Because the handler can’t register the form internally until after the form is instanced, and because the form’s native Open/Load (and possibly Current) events will fire at instantiation, we have no way to slip in before these, so to speak. This usually shouldn’t be an issue, but be aware that the handler will not be attached until after these events have run. If you must have the handler attached beforehand, use UI.PreRegisterForm() from within the form’s Open or Load event.

Usage

For dialog or fast handler initialization, use this in the Open or Load event:

UI Module

(standard module, copy/paste or import… if you change the module name do a Find/Replace on “UI.” to whatever module name you change it to

FormHandler module

(Class Module, copy/paste or import)