Detect which subform is active

Posted on 2011-10-29
Last Modified: 2012-05-12
In Access 2003, I have a form Main that is a holder for several subforms, including Action and Task.  (They aren't linked to the MAIN form, it's just a shell/holder.  I have these subforms on fixed positions on the Main form instead of floating for various reasons that are *currently* more important that the feature I'm asking about here).

I would like to be able to set a global string variable TaskOrAction to either "Task" or "Action" when that respective subform is activated.

I need to find an event that I can use to detect that the subform is activated, or, more specifically, if ANY control on that subform is used, so I can set the value of TaskOrAction (which I use later to inform a query of whether to look for a key value in ...Task or Action).

I tried a few things that didn't work:
1)  According to MS, "Activate" does not work for subforms,

2)  According to this,
GotFocus does not work unless you use the record selector (though I did not see that happen either, when I tried GotFocus

3)  Enter only works for specific controls, and I need to set the variable if any control on the subform is used, or if the subform record selector is keyed.

I built a little test bed, attached.

Any suggestions on this would be appreciated.


Question by:codequest
    LVL 47

    Accepted Solution

    Have you looked at the subform controls "Enter" and "Exit" events.  You could declare a variable in the Main form and use the subforms Enter and Exit events to set the value of that variable.

    Depending on where you want this code to fire, say in a public module, you could use something like:


    If the focus is on one of the controls on the main form, this would print out "Main", but if the focus is one of the textboxes in a subform, it would give the name of the subform.
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    Try this:

    LVL 75

    Assisted Solution

    by:DatabaseMX (Joe Anderson - Access MVP)
    A slightly better version ...
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    "1)  According to MS, "Activate" does not work for subforms, "
    That is correct.

    We are basically using


    keeping in mind that a subform IS a Control ... a container for an actual Form - to be used as a subform.

    LVL 2

    Author Closing Comment

    Works slick.  Thanks!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now