1
votes

I have a userform that I have created that contains a number of TextBoxes. Each of these fields are named name1, name2, name3, etc.

This userform is called from a Word macro used to process data. Depending on the data in the file the macro is run on, I want content from an array to be displayed in each of those TextBoxes.

What I want to be able to do is dynamically reference the form TextBoxes so that I can populate them from within a loop, as opposed to having to do a separate if statement for each individual value/TextBox.

So for example, instead of having to do something like this:

For p = 1 To count
   if p=1 then
       dForm.name1.Text=myVar(p)
   end if
   if p=2 then
       dForm.name2.Text=myVar(p)
   end if
   .
   .
   .
   etc.
Next p

I want to be able to something much more simple and efficient like:

For p = 1 To count
   tempString = "name" & p
   dForm.tempString.Text = myVar(p)
Next p

Unforunately though, I cannot figure out how to do this.

Is this possible? I had been hoping that something similar to what can be done in Actionscript would work, but it didn't (in Actionscript I would simply just do dForm["name"+p].Text = myVar[p]).

Any ideas/suggestions? Nothing I've tried has worked, and I haven't been able to find anything online about this. I'm sure there has got to be some work around to avoid having to do an incredible number of if statements (the 'name' TextBoxes' is just one of many repeating TextBoxes that is part of my form, so having to do if statements for all of them would take forever) ...

1

1 Answers

1
votes
UserForm1.Controls("name" & p).Value = myVar(p)

or...

Private Sub FillTextBoxes()
    Dim ctl As Control

    For Each ctl In UserForm1.Controls
        If TypeName(ctl) = "TextBox" Then
            If ctl.Name Like "name*" Then
                ctl.Value = myVar(p)
            End If
        End If
    Next
End Sub