Results 1 to 3 of 3

Thread: Existing in collection ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Qu├ębec, Canada
    Posts
    241

    Post

    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.

  2. #2
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386

    Post

    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...

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    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:
    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width