Results 1 to 4 of 4

Thread: trouble with control collection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Location
    New York City
    Posts
    21

    trouble with control collection

    why won't this work:

    VB Code:
    1. dim coll as collection
    2. dim og as OptionButton
    3. dim cb as ComboBox
    4. dim TB as TextBox
    5.  
    6. for each og in me.controls
    7. coll.add og
    8. next
    9.  
    10. for each.... ' same with textboxes and comboboxes
    11.  
    12. but it gives me an error.
    13.  
    14. even this won't work:
    15.  
    16. for each cb in me.controls
    17. ....
    18. ctr.enabled=false
    19. next cb

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: trouble with control collection

    The For Each statement enumerates every item in the collection, not those of a specific type. A variable declared as a specific type can only be set to an instance of that type. Use the generic Control object type and then check for the "type of" control

    VB Code:
    1. Dim oCtl as Control
    2.  
    3. For each oCtl in Me.Controls
    4.     oCtl.Enabled = False 'assumes all controls have an enabled property.
    5.  
    6.     If Typeof oCtl is OptionButton then
    7.        oCtl.Value = False
    8.     ElseIf Typeof oCtl is TextBox then
    9.        oCtl.Text = ""
    10.     End If
    11.  
    12. Next

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

    Re: trouble with control collection

    And when you want to use a collection you have to create the object and not just define it so you eaither have to do

    VB Code:
    1. Dim coll As [HL="#FFFF80"]New [/HL]Collection
    2. coll.Add "something"

    or

    VB Code:
    1. Dim coll As Collection
    2. Set coll = New Collection
    3. coll.Add "something"

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Location
    New York City
    Posts
    21

    Re: trouble with control collection

    thanks... i was thinking along those lines - but i was hoping that VB had a .type property, rather than a TypeOf method (sorta like C)

    guess i was wrong

    hope it works - thanks

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