• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 122
  • Last Modified:

Detect if a control exists

Hi,

I have a control array of labels on a form.  Based on what "Choice" the user enters in a text box, I have the respective label highlight.  How can I first test to see if the label exists?

Thanks,

ssteeves
0
ssteeves
Asked:
ssteeves
1 Solution
 
QJohnsonCommented:
As with most of these "exists" questions, you have 2 options.

The elegant one is to iterate the Controls collection looking for one with your control's name.  

The inelegant (but arguable faster) one is to simply set an error trap for the expected error and try and access the control directly, gracefully continuing if it isn't there and making your assignment statement if it is.

Do you need code examples?
0
 
QJohnsonCommented:
Actually, in the case of a control array, if you are SURE that at least one of the items is there, you can just iterate the control array looking for your index (or some other property that identifies a particular member).  Keep in mind that the indexes are necessarily sequential.  

The presence of item 6 doesn't mean that item 4 is present, for example.

This will be faster than iterating the entire Controls collection, for sure.
0
 
aelatikCommented:
If IsObject(Label1) Then
    MsgBox "Yes"
Else
    MsgBox "NO"
    End If
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
normanpatersonCommented:
For Each Control in frmMyForm.Controls
     If Instr(Control.Name,"myControlName") > 0 then
         Label1.Fontbold = true
     End If
Next Control

=================================================
In the above example, "frmMyForm" is the name of your form, and "mycontrolName" is the name of the controls in your control array (without the trailing index)
i.e.
mycontrolName(0)
mycontrolName(1)
mycontrolName(2)

Would be an example of a valid control array.  The Instr simply sees if there is a control on the form with that name.  If it exists, Instr will be greater than zero.
0
 
ssteevesAuthor Commented:
All good suggestions, but this one works fine.  Thanks.
0
 
QJohnsonCommented:
Do I understand correctly that the existence of the ARRAY is not your concern?  You need to know if a particular member of that array is present correct?  If so, routines that show the name match only are of no use, right?

You need to test for the index as well if this is the case.
0
 
ssteevesAuthor Commented:
Just curious, but how do I test for the existence of an array (of controls) element?
0
 
QJohnsonCommented:
This will do it:

Public Function ControlArrayItemExistsOnForm( frm As Form, strCtlName As String, intIndex as Integer) as Boolean

Dim ctl    As Control
Dim bFound As Boolean

bFound = False

For Each ctl in frm.Controls
      If ctl.Name = strCtlName Then
            If ctl.Index = intIndex Then
                  bFound = True
                  Exit For
            End If     '   did we find the item of the array?
      End If      '  did we find the array?
Next

ControlArrayItemExistsOnForm = bFound

End Function

You can paste the routine into a module and call it from a form with this code:

bMyControlItemExists =  ControlArrayItemExistsOnForm(me, "ControlName", intIndex)

.... or you can simply modify the code to exist in a routine in the form or use the core of it in-line with existing code depending on what kind of re-use capability you want.

Q
0
 
ssteevesAuthor Commented:
Thanks!  That was what I was originally hoping for...
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now