?
Solved

Detect if a control exists

Posted on 2003-03-18
9
Medium Priority
?
121 Views
Last Modified: 2010-04-07
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
Comment
Question by:ssteeves
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Accepted Solution

by:
QJohnson earned 200 total points
ID: 8161444
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
 
LVL 3

Expert Comment

by:QJohnson
ID: 8161468
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
 
LVL 14

Expert Comment

by:aelatik
ID: 8161494
If IsObject(Label1) Then
    MsgBox "Yes"
Else
    MsgBox "NO"
    End If
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:normanpaterson
ID: 8161542
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
 
LVL 1

Author Comment

by:ssteeves
ID: 8161570
All good suggestions, but this one works fine.  Thanks.
0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8161588
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
 
LVL 1

Author Comment

by:ssteeves
ID: 8173292
Just curious, but how do I test for the existence of an array (of controls) element?
0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8174342
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
 
LVL 1

Author Comment

by:ssteeves
ID: 8174537
Thanks!  That was what I was originally hoping for...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question