Testing for the existence of an ActiveControl

Posted on 2006-04-29
Last Modified: 2012-05-05
If you use aForm.ActiveControl or Screen.ActiveControl when there isn't one, an error occurs.  How do test for the presence of an active control before trying to refer to it?
Question by:xerle
    LVL 58

    Expert Comment


    The standard answer for that is "use error management", I guess:

    Sub TestThis()

        Dim ctl As Control

    On Error Goto TestThis_Error

        Set ctl = Screen.ActiveControl

        ' [...]


        Exit Sub


        If Err.Num = <insert number here> Then
            ' take propert action, e.g.
            Resume TestThis_Exit
        ElseIf Err.Num = ....
            ' ...
        End If
        MsgBox Err.Description, , "Error " & Err.Num
        Resume TestThis_Exit

    End Sub

    You code should have this anyway ;)

    LVL 58

    Accepted Solution

    If you prefer, you can do this:

    Function SafeActiveControl() As Control
    On Error Resume Next
        Set SafeActiveControl = Screen.ActiveControl
        If Err Then Err.Clear
    End Function

        Set ctl = SafeActiveControl
        If ctl Is Nothing Then
            ' failed!

    Good luck!
    LVL 1

    Author Comment


    I modified your last suggestion a little as follows:

       Function ActiveControlExists(aForm As Form) As Boolean

           'Test for the existence of a valid ActiveControl.

           On Error GoTo HandleErr

           Dim aControl As Control
           Set aControl = aForm.ActiveControl
           ActiveControlExists = True

           Exit Function

           ActiveControlExists = False

       End Function

    LVL 58

    Expert Comment

    Sure, looks good. ;)

    Probably not important, but an error handler should end with either a Resume. clearing of the error, or raising a new one...

           ActiveControlExists = False
           Err.Clear   ' <--- added

       End Function

    But again, I'm not sure this makes any difference...

    Happy programming!

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This isn't a frequent question on EE. I must have seen it three or four times (among several thousand questions). However, I use this trick quite often, most frequently as a delayed Current event. A form does not expose it's calculation dependenc…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    728 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

    15 Experts available now in Live!

    Get 1:1 Help Now