Userforms Textbox control

  • Thread starter elizabeth.cornelius
  • Start date
E

elizabeth.cornelius

Hi Guys

I am trying to use an array to select various textboxes on a userform
but keep getting an error when compiling the code saying 'Method or
data member not found'. I understand the essage but cannot work out
how to overcome it.

Here is the code, it is pretty simple as it is just trying to sort out
the code.

Thanks

Damon & Elizabeth

****************CODE****************

Private Sub CommandButton1_Click()

Dim myTest, myTest1
Dim i As Integer


myTest = Array("txt1", "txt2")
myTest1 = Array("Goodbye", "So long", "Adiue")

For i = LBound(myTest) To UBound(myTest)

UserForm1.myTest(i) = myTest1(i)

Next i
End Sub

*******************END CDE**********************
 
J

Jay Freedman

Regardless of what you may think you put into the array myTest, what
you actually have is an array of strings stored in a Variant. You know
that those strings are the names of text boxes in the userform, but
VBA doesn't know how to figure that out. :)

The userform does have a Controls collection that contains all the
controls (text boxes, buttons, combo boxes, etc.). That collection has
a .Item method that takes either an index number or the name as a
string, and returns the corresponding control. So you can use

UserForm1.Controls.Item(myTest(i)) = myTest1(i)

and it will work. As a short of shorthand, you can write the same
thing as

UserForm1.Controls(myTest(i)) = myTest1(i)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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