?
Solved

Testing for the existence of an ActiveControl

Posted on 2006-04-29
4
Medium Priority
?
399 Views
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?
0
Comment
Question by:xerle
  • 3
4 Comments
 
LVL 58

Expert Comment

by:harfang
ID: 16571237
Hello,

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

    ' [...]

TestThis_Exit:

    Exit Sub

TestThis_Error:

    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 ;)

Cheers!
(°v°)
0
 
LVL 58

Accepted Solution

by:
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!
(°v°)
0
 
LVL 1

Author Comment

by:xerle
ID: 16571331
Harfang,

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

   HandleErr:
       ActiveControlExists = False

   End Function


Thanks,
Erle
0
 
LVL 58

Expert Comment

by:harfang
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...

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

   End Function

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

Happy programming!
(°v°)
0

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