Consulting

Results 1 to 12 of 12

Thread: Solved: Sequence of controls during For-Next loop

  1. #1
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location

    Solved: Sequence of controls during For-Next loop

    I have a series of CheckBox controls on a form. Some of them are dependent on the value of others. Is there a property (that I can modify) that determines the sequence that the controls are retrieved during this loop?

    [VBA]For Each ctlInForm In frmItems.Controls
    Next ctlInForm[/VBA]

    I was looking for an eloquent solution, but it's looking like I'll need to brute force it.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Its the order they are added.

    [vba]
    For Each ctlInForm In frmItems.Controls
    MsgBox ctlInForm.Name
    Next ctlInForm
    [/vba]

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You can also loop through them like this:
    [VBA]
    For x = 1 To 10
    MsgBox Me.Controls("CheckBox" & x).Value
    Next x

    [/VBA]

    Assuming their names are CheckBox1, CheckBox2 ...

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi,
    If your controls are not sequential, you could use an array of the selected order. This could be modified to suit named controls if required.
    MD
    [VBA] CBs = Array("3", "1", "2")
    for each c in CBs
    MsgBox Me.Controls("CheckBox" & c).Value
    next
    [/VBA]

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    The original post regarded conditional logic on a control. if ONE is a particular value then make another one a particular value.

    Why not just do it by explicit naming of the control? If controlX = blah, then controlY = blahblah2. Am I missing something, ....again?

    Alternatively, you could set the TabIndexes of the checkboxes, and use that.

    [vba]
    Dim myControl As Control
    For Each myControl In frmTestTabOrder.Controls
    If myControl.TabIndex = 2 Then ' a specific checkbox
    If Me.Controls(myControl.Name).Value = True Then
    ' some other control = true
    ' or whatever
    End If
    End If
    Next
    [/vba]

  6. #6
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location
    Gerry, that's what I was planning to do, except, like DrJ stated, they are sequenced by the order they were added to the form which is not the order I need to get them in. Some of the functions are inter-dependent and if processed in the wrong order cause an incorrect result. I could delete and re-add them in the correct order, but I'd rather not rely on something that I can't control.

    I'm gathering that, other than adding the controls in the correct order or putting them into an array before I process them, that there is no way to control the order that they are retrieved. Oh well, guess I'll re-write my code.

    Thanks for your help!

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Just name them in the order you want, sequentially. Then use this:

    [vba]
    For x = 1 To 10
    MsgBox Me.Controls("CheckBox" & x).Value
    Next x
    [/vba]

    Assuming they are named CheckBox1, CheckBox2, CheckBox3...

  8. #8
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hey, VA, did you ever get this one resolved?

    ~Anne Troy

  9. #9
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location
    Quote Originally Posted by Dreamboat
    Hey, VA, did you ever get this one resolved?

    Well, yes and no. As DrJ pointed out, when you loop through the controls using the "For Each" statement, they get processed in the order that they were added to the form. This is not a setting that can be changed (except to re-create the form and add the controls in exactly the order you need to process them). The pain of doing this, let alone the anticipated pain of adding something in the middle later on, made me shy away from doing this.

    I finally ended up handling two different ways (Ok, I was playing... ). For one set of controls I put a number in the .tag property and added the controls to an array with the index being the value of the .tag property. Then I just looped through the array from 1 to n and everything was neatly ordered.

    In other cases I just coded multiple If statements in the order I needed them.

    So, yes, I'm done. But no, it's not as elegant a solution as I would have liked.

    Case closed, on to new projects!

  10. #10
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    I was hoping to get a neat way around that, too. Because I just love For each - loops as they make later additions quite flexible, and I just have the same problem as you do.

    Maybe we should send our complaints to Bill, then maybe VB .NET#++ will have an userdefined order available.

  11. #11
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Steiner
    I was hoping to get a neat way around that, too. Because I just love For each - loops as they make later additions quite flexible, and I just have the same problem as you do.

    Maybe we should send our complaints to Bill, then maybe VB .NET#++ will have an userdefined order available.
    VB .NET does not have Control Arrays as in VB 6, but it does allow for arrays of controls.

  12. #12
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location
    Quote Originally Posted by Steiner
    Maybe we should send our complaints to Bill, then maybe VB .NET#++ will have an userdefined order available.
    Heck, it's already there! They just need to expose it as a property! I guess we can dream... :grdmartie

Posting Permissions

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