trying to check if an object exists?

homer2002

Registered User.
Local time
Today, 13:31
Joined
Aug 27, 2002
Messages
152
Is there a nice easy way to check if an object exists on a form?

For example, could I check to see if a label called lblMyLabel exists?
 
Put this code in your form and call it by passing the name of the control you want to check.

I've only written this to work on one form but it can be adapted for all forms.

Code:
Private Function ExistsOnForm(ByVal strName As String) As Boolean
    Dim ctl As Control
    For Each ctl In Me
        If ctl.Name = strName Then
            ExistsOnForm = True
        End If
    Next
End Sub


[EDIT] - Silly little mistake fixed.
 
Last edited:
Mile, slight correction in your Dim statement:
Dim ctl As Control :)

Also, you can try to use a more "direct indirect" route to finding out if the control exists by trying to refer to one of it's properties and seeing if an error was raised like this:
Code:
Function TestLabelName(strLabelName As String) As Boolean
    On Error Resume Next
    Debug.Print Forms("formname").Controls(strLabelName).Caption
    'or just use Me(strLabelName) if your referring to the current form
    TestLabelName = (Err.number = 0)
End Function
 
Last edited:

Users who are viewing this thread

Back
Top Bottom