Variable variable name?!!?

M

MattShoreson

say you have 3 textboxes called txt1, txt2 and txt3.
It's possible to cycle thru these controls by using the following...

for i = 1 to 3
msgbox .controls("txt" & i).value
next i

can this be done with variables names?
i.e. variable1, variable2, variable3

help gratefully accepted!
Thanks,
Matt
 
D

Die_Another_Day

That should work.
variable1 = "txt1"
MsgBox Userform1.Controls(variable1).value
 
B

Bob Phillips

No, not possible.

Create an array, and cycle through that

aryValues = Array(1,2,3,4)

For i = Lbound(aryValues) to Ubound(aryValues)
MsgBox aryValues(i)
Next i

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"MattShoreson" <[email protected]>
wrote in message
news:[email protected]...
 
M

MattShoreson

thanks bob.

Realised that I had to do it this way. Think it's getting a little late
in the day!
A collection of variables = an array!
 
G

Gary Keramidas

this should do what you want. mine are textbox1 thru textbox3

Private Sub CommandButton1_Click()

For i = 1 To 3
MsgBox Me.Controls("textBox" & i)
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top