How to check control existence on a form in VB?

manojkurhade
manojkurhade used Ask the Experts™
on
Hi,

Can anyone please tell me, how can i can check whether a particular control exists or not, on a form in visual basic.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You need to use the Controls collection of the form, and loop through looking for the control you want.

eg

Dim myCtl As Control
Dim fControlExists as boolean

' set up the fControlExists variable
fControlExists = false

' loop through all the controls on the form
For Each myCtl In myForm.Controls
    If myCtl.Name = "The control I'm Looking For" Then
        ' Use myCtl for whatever purpose you want eg
        fControlExists = true
    End If
Next myCtl

if fControlExists then
    Msgbox "The control Exists"
else
    Msgbox "The control does not exist"
end if



I don't know of any easier way of getting a particular control.

Hope this helps,

Ken
I learned this one from Tandrei
    If VarType(Command3) = vbEmpty Then
        MsgBox "Command3 does not exist"
    End If
I just tried that, twalgrave - unfortunately it fails to compile because Command3 is referred to directly but doesn't exist.

I usually wrap up a brute-force approach to this sort of thing:

...
    If ControlExists("Command1") Then
        MsgBox "Command1 exists"
    End If
...
Public Function ControlExists(ByVal CtlName As String) As Boolean

    'Suppress errors so this returns default of False if
    'the named control doesn't exist, but succeeds and
    'returns True if it does
    On Error Resume Next
    ControlExists = (TypeOf Controls(CtlName) Is Control)

End Function
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

(Apologies if this comes out twice ... it doesn't seem to have added it)

I just tried that, twalgrave - unfortunately it fails to compile because Command3 is referred to directly but doesn't exist.

I usually wrap up a brute-force approach to this sort of thing:

...
    If ControlExists("Command1") Then
        MsgBox "Command1 exists"
    End If
...
Public Function ControlExists(ByVal CtlName As String) As Boolean

    'Suppress errors so this returns default of False if
    'the named control doesn't exist, but succeeds and
    'returns True if it does
    On Error Resume Next
    ControlExists = (TypeOf Controls(CtlName) Is Control)

End Function
I don't have that problem with compiling (I'm using VB6).


Here is my entire code:

I have a single form and there's NOTHING on it.

Private Sub Form_Load()
   If VarType(Command3) = vbEmpty Then
       MsgBox "Command3 does not exist"
   End If

End Sub
Also, I would recommend that if you use DaveRiley's code that you put an err.clear before the end sub.  Sometimes errors propogate in the unlikeliest places.  I recommend that any time you use On Error Resume Next, that you clear the error if you get one (In this case, even if you don't)

Ah!  The difference is I have "Option Explicit" set so it checks at compile-time that everything you refer to does in fact exist.

You're right though, without "Option Explicit" yours works a treat.

And I totally agree about using Err.Clear too.  For the record (in case anyone's here having hit this problem) there's a "feature" where an error suppressed by On Error Resume Next in one component will be raised in the code which calls it ... but only when it's compiled(!) debugging in the IDE doesn't suffer the same problem.
Good catch.  Normally I have Option explicit set on.  I don't know how/why it got turned off, but it did.  I do not recommend turning off option explicit just so my code will work.  Option Explicit is an invaluable tool.

Author

Commented:
Hi DaveRiley,

Thanks for the solution.  It has worked very fine for me.

Warm Regards
Manoj

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial