Testing for the existence of an ActiveControl

Posted on 2006-04-29
Medium Priority
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
  • 3
LVL 58

Expert Comment

ID: 16571237

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

harfang earned 500 total points
ID: 16571239
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!

Author Comment

ID: 16571331

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

ID: 16571376
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

807 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