Determine which subform has focus

Posted on 2007-10-20
Last Modified: 2008-01-09
I have two subforms on a form.  I have a function that runs, but I want just a couple of lines to not be ran if one subform has the focus rather than the other one.  I want to insert an IF statement, but what syntax would I use to determine which subform has focus:

Example:  If subform1 has focus then...
Question by:sf_garrison
    LVL 3

    Accepted Solution

    I don't believe there is a way to determine whether a subform has the focus, and generally it won't be the subform that has the focus, but rather one of the controls on it.  Here is a quick and easy way to accomplish what you want though:
    On The Main Form create an unbound checkbox and set it's visible property to false, set it's name to whatever you want.  Let's say you call it 'Check1'
    In the main form create an Event Procedure in each subform control's 'OnEnter' property that will turn on or off the checkbox depending on which form it is.

    Private Sub Subform1_Enter()
       Me.Check1 = True
    End Sub

    Private Sub Subform2_Enter()
       Me.Check1 = False
    End Sub

    This will set the checkbox to true whenever you enter subform1 and turn it off whenever you enter subform2.  You can then test the state of the checkbox in your code.

    If Me.Check1 = True Then ......

    Author Comment

    Thanks for the quick reply on a Saturday night !
    LVL 3

    Expert Comment

    Glad to help
    LVL 61

    Expert Comment

    The ActiveControl property tells you exactly which control has the focus.  You can also use that to determine if the focus is on (or within) a particular subform:

    If Forms!MyformName.ActiveControl.Name = "SubForm1"  then
    end if


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    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…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    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

    16 Experts available now in Live!

    Get 1:1 Help Now