PDA

Click to See Complete Forum and Search --> : Existing in collection ?


David Laplante
Nov 11th, 1999, 09:51 PM
Hi there.
I would like to know if it is possible to check if, let's say the control "txtName", is in the me.controls collection. If I try affecting a value to this control and it doesn't exist, of course I get an error. Yes I could loop through the collection to check the name of the control, but I want to avoid this.

Thanks.

Crazy D
Nov 11th, 1999, 10:30 PM
You mentioned the two ways to check... get or set a property of the control, and if it doesn't exist, you get an error, or loop thru all the controls...
AFAIK there's no function like "IfControlExists(ControlName)" .. and if there is, it does loop thru all the controls...

MartinLiss
Nov 11th, 1999, 10:38 PM
You can use the objects' Tag property (which is a use-it-as-you-like property supplied my MS) to track if an object is in the collection by doing something like the following:
Option Explicit
Dim MyCollection As New Collection

Private Sub Form_Load()
AddToCollection Text1
AddToCollection Text2

End Sub


Private Sub Text1_Click()
If Text1.Tag = "Yes" Then
MsgBox "I'm in the collection"
End If

End Sub

Public Sub AddToCollection(obj As Object)
MyCollection.Add obj
obj.Tag = "Yes"

End Sub


------------------
Marty