ACC: Macro on Subform Control Executed by Main FormID: Q100150
|
Moderate: Requires basic macro, coding, and interoperability skills.
If a macro is attached to the OnEnter or OnExit property of the first
control in the tab order of a subform, the macro will run each time
you move from one record to another in the main form.
In addition, a macro attached to the OnEnter property of the first
control in the tab order of the subform will run when the main form is
opened, and a macro attached to the OnExit property of the first
control in the tab order of the subform will run when the main form is
closed.
NOTE: This behavior is also true for Visual Basic for Applications code
(or Access Basic code in Microsoft Access 2.0 or earlier) attached to the
OnEnter or OnExit property.
To cause a macro to run only when you are actually moving in and out of the subform, use a test condition with a flag. A macro can set a flag on the main form to indicate whether the subform was entered by moving into the subform from the main form. The macro attached to the subform's OnEnter and OnExit properties can check this flag to see if you really did move into the subform and continue running if appropriate. The following steps demonstrate how to set up and test for this condition.
Macro Name Action
-----------------------
Set SetValue
UnSet SetValue
Subform Flag Actions
-----------------------
SetValue
Item: [Subform Flag]
Expression: True
SetValue
Item: [Subform Flag]
Expression: False
Form: MyForm
Text Box: Subform Flag
Name: Subform Flag (in version 1.x, ControlName: Subform Flag)
Visible: No
Default Value: False
Form: MyForm Subform
On Enter: Subform Flag.Set
On Exit: Subform Flag.UnSet
Condition Action
-----------------------------------------
Parent![Subform Flag] = False StopMacro
MacroName Action
--------------------
Test MsgBox
Test Actions
--------------------
MsgBox
Message: Testing!
Form: Orders Subform
OnEnter: Test
OnExit: Test
Additional query words: exit enter
Keywords : FmsEvnt
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: March 24, 1999