Results 1 to 4 of 4
  1. #1
    Join Date
    May 2011
    Posts
    47

    Unanswered: Looping Through TextBoxes Which Include a Number

    I have a number of textboxes on a form that I would like to loop through and set values in. The textbox names contain a number. For example Me.txt_box_1.value, Me.txt_box_2.value, etc. Since I want to loop through the textboxes, I would like to replace the number at the end of the textbox name with an iteration value (such as i) so that my code would (in theory) look like:

    For i = 1 to some number
    Me.txt_box_i.value = some calculation
    Next i

    I've tried a number of ways to replace the number with an iteration value (ampersands etc.) but cannot solve this problem thus far.

    Would very much appreciate help with this.

    Thanks,
    Jack Kent

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    If the structure of the name for a textbox control is always "Txt_Box_" followed by a numeric value and if the numeric values are contiguous, you could try (here for Txt_Box_1 to Txt_Box_12):
    Code:
        Dim i As Integer
        
        For i = 1 To 12
            Me.Controls("Txt_Box_" & i).Value = <Value>
        Next i
    For iterating the controls collection and find the textboxes matching your naming convention you could use:
    Code:
        Dim ctl As Control
        Dim intControlIndex As Integer
        
        For Each ctl In Me.Controls
            If Left(ctl.Name, 8) = "Txt_Box_" Then intControlIndex = Val(Mid(ctl.Name, 9))
        Next ctl
    Have a nice day!

  3. #3
    Join Date
    May 2011
    Posts
    47

    For Next with Iterations

    Thanks so much. I will try this.

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

Posting Permissions

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