Link to home
Start Free TrialLog in
Avatar of manojkurhade
manojkurhade

asked on

How to check control existence on a form in VB?

Hi,

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

Avatar of KenTweedie
KenTweedie

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
ASKER CERTIFIED SOLUTION
Avatar of DaveRiley
DaveRiley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of manojkurhade

ASKER

Hi DaveRiley,

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

Warm Regards
Manoj