Results 1 to 5 of 5

Thread: Control array object from form Controls collection?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Question Control array object from form Controls collection?

    How can I access a control array object as an item on a form, vs. explicitly by name?

    e.g. I have a TextBox "Text1", with index=0 on a form (that's it)
    but the following code fails with Error 344, "Must specify index for object array"

    Code:
        Dim xx As Object
    
        For Each xx In Me.Controls
            Load xx(1)
        Next xx

    FWIW
    Load Text1(1) works fine.
    ? TypeName(Text1) = Object
    ? TypeName(xx) = TextBox

    Apparently, in the For Each loop it's giving me actual instances of the textbox control, rather than the control array itself.

    Thanks, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  2. #2
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401

    Re: Control array object from form Controls collection?

    Originally posted by DaveBo
    How can I access a control array object as an item on a form, vs. explicitly by name?

    e.g. I have a TextBox "Text1", with index=0 on a form (that's it)
    but the following code fails with Error 344, "Must specify index for object array"

    Code:
        Dim xx As Object
    
        For Each xx In Me.Controls
            Load xx(1)
        Next xx

    FWIW
    Load Text1(1) works fine.
    ? TypeName(Text1) = Object
    ? TypeName(xx) = TextBox

    Apparently, in the For Each loop it's giving me actual instances of the textbox control, rather than the control array itself.

    Thanks, DaveBo
    Modify ur code thus:

    Dim xx As Object

    For Each xx In Me.Controls

    Load xx(1)
    Next xx
    Enjoy!!!
    apps_tech

  3. #3
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401

    Re: Re: Control array object from form Controls collection?

    Originally posted by apps_tech
    Modify ur code thus:

    Dim xx As Object

    For Each xx In Me.Controls

    Load xx(1)
    Next xx
    sorry before i enter the code it got submitted. here is the one i wanted to write:

    Dim xx As Object

    For Each xx In Me.Controls
    If typeof(xx) is textbox then
    Load xx(1)
    end if
    Next xx

    i guess it should work...
    Enjoy!!!
    apps_tech

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Not quite

    It is a textbox, and that seems to be the problem.

    If you put a regular textbox, Text1 on a form
    and then a 2nd, Text2, with index=0.
    Then compare the properties/methods of Text1 and Text2, you'll see that Text2 (without the (0) index specified) looks more like a collection.

    Thanks, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  5. #5
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    You can't access a control array from the Forms Control collection. You can get the controls atributes for example Name,BackColor,Text etc...

    VB Code:
    1. Dim xx As Control
    2.  
    3. For Each xx In Me.Controls
    4.  If TypeOf xx Is TextBox Then
    5.   MsgBox xx.Name
    6.   MsgBox xx.Text
    7.   MsgBox xx.BackColor
    8.  End If
    9. Next

    but you can't access that controls collection properties such as xx.ubound or xx.count etc... which means you can't load another control.

    For instance this won't work

    VB Code:
    1. Dim xx As Control
    2.  
    3. For Each xx In Me.Controls
    4.  If TypeOf xx Is TextBox Then
    5.   Load xx.Name(1)
    6.  End If
    7. Next

    Even though it is saying Load Text1(1) it still won;t work because you are looping thru the Forms control array and not the controls array itself.

    Atleast that's what I think! I could be totaly wrong!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


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