PDA

View Full Version : [SOLVED:] Userform TextBox Loop Syntax



BrI
07-30-2019, 05:02 AM
I have a multipage userform where I can successfully insert text into a single TextBox as follows:


Me.MultiPage1.Pages(0).txtMainAdd_1.Text = "Insert this text"

I want to do the same thing in a loop for several TextBoxes but cannot get correct syntax. I have tried variations of the code below without success. Error says could not find object.


For i = 1 To 6


Me.("MultiPage1.Pages(0).txtMainAdd_") & i.Text = "Insert Text via LOOP"

'- or -

Me.Controls("MultiPage1.Pages(0).txtMainAdd_" & i).Text = "Insert Text via LOOP"


Next i

How can I fix?

Bob Phillips
07-30-2019, 05:23 AM
Try this


For i = 1 To 6

With Me.Controls("MultiPage1").Pages(i - 1)

For Each ctl In .Controls

If ctl.Name = "txtMainAdd_" & i Then

ctl.Text = "Insert Text via LOOP"
End If
Next ctl
End With
Next i

BrI
07-30-2019, 05:47 AM
That works - thanks very much!

For the record I changed the first line to:

With Me.Controls("MultiPage1").Pages(0)

Bob Phillips
07-30-2019, 10:50 AM
Yeah, I was not sure if all the textboxes were on one page or spread over the 6 pages, so I took a punt :rotlaugh: