Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 208511 - Last Review: July 13, 2004 - Revision: 1.1
ACC2000: Macro on Subform Control Executed by Main Form
This article was previously published under Q208511
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 that 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.
The macro attached to the
OnEnter property of the first control in the subform will not run if you enter the control from the main form and the control is in the selected record on the subform (denoted by a triangle in the record selector). As a result, the macro will not run when you enter
the subform by pressing TAB, or by clicking the first control in the
selected record on the subform. However, the macro will run if you click
this control on a different record in the subform.
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.
- Create the following macro called Subform Flag:
Macro Name Action
-----------------------
Set SetValue
UnSet SetValue
Subform Flag Actions
-----------------------
SetValue
Item: [Subform Flag]
Expression: True
SetValue
Item: [Subform Flag]
Expression: False
- For a form named MyForm, add an unbound text box to the main form with the following properties:
Form: MyForm
Text Box: Subform Flag
Name: Subform Flag
Visible: No
Default Value: False
- Click the subform control on the main form and change the following
properties:
Form: MyForm Subform
OnEnter: Subform Flag.Set
OnExit: Subform Flag.UnSet
- Add the following macro action to the top of the macro that is
attached to the OnEnter or OnExit property of the first control in the tab order of the subform. The macro will not perform any other actions if the subform flag is not set.
Condition Action
-----------------------------------------
Parent![Subform Flag] = False StopMacro
Steps to Reproduce Behavior
- Open the sample database Northwind.mdb and create the following macro called Test:
MacroName Action
--------------------
Test MsgBox
Test Actions
--------------------
MsgBox
Message: Testing!
- Open the Orders Subform form and change the OnEnter and OnExit properties of the ProductID box to Test, as follows:
Form: Orders Subform
OnEnter: Test
OnExit: Test
- Save the Orders Subform form, open the Orders form, and move among the records. Note that the message "Testing!" appears twice for each record.
APPLIES TO
- Microsoft Access 2000 Standard Edition
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate