Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if control exists

Status
Not open for further replies.

Glasgow

IS-IT--Management
Joined
Jul 30, 2001
Messages
1,669
Location
GB
Is there an easy way, without using an error trap or looping through the name properties within the controls collection of a form, to determine whether a specific control has been defined on that form?

Thanks in advance.

 
Not that I can think of... I suppose you might try and get at the form's property bag, but I seem to remember your having stated "an easy way"...

What's wrong with the first two?

mmilan
 
Thanks mmilan - I'm not a big fan of error traps and I always prefer to find an alternative even if it involves a few extra lines of code. Otherwise I just really wanted to know whether I was missing anything obvious. For example, Typename(NonExistentControl) returns Empty in immediate window but crashes in code - otherwise that might have been a nice option.
 
Put the error handler in a function, which hides the 'messyness'

Try this:


MsgBox ControlExists("command2")


Private Function ControlExists(szName As String) As Boolean
ControlExists = False
On Error GoTo nothere

Dim c As Control
Set c = Me.Controls(szName)
ControlExists = True
Exit Function


nothere:
Exit Function
End Function
 
Thanks Jeff.

I could probably get away with:

Private Function ControlExists(szName As String) As Boolean
On Error Resume Next
If Me.Controls(szName).Name<>&quot;&quot; Then ControlExists=True
End Function

Though I haven't tested it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top