1

I want to create large number of textboxes with loop. lets say I want to create one textbox and I tried this code ;

        UserForm1.Controls.Add "Forms.TextBox.1", "Txt", True

with this code I can create textbox but I can't reach the properties of textbox .For example When I try this ;

UserForm1.Controls.Add "Forms.TextBox.1", "Txt", True
UserForm1.txt.Text = "asdasd"

It gives "compile error. Method or Data member not found". How can I reach the properties of this textbox?

Ozgur G.
  • 11
  • 3
  • So you have several textboxes all with the same name? Try giving them different names by using as the name, where would increment at each loop. That was you would be able to refer to them seperately. I don't know if that's the source of the error, but you should do it either way. – David G Jul 10 '15 at 14:20
  • Thanks for the reply. Actually I'am doing it just like you said . , My code in the loop is UserForm1.Controls.Add "Forms.TextBox.1", "Txt" & x & y, True but I think If i can solve how to reach just one of them I can modify all .thats why I didn't write the whole code – Ozgur G. Jul 10 '15 at 14:25
  • 1
    Have you tried this syntax? UserForm1.Controls("Txt" & x & y).Value = "abcde" – David G Jul 10 '15 at 14:29
  • Now I tried . but not worked – Ozgur G. Jul 10 '15 at 14:33
  • See http://stackoverflow.com/questions/10592641/assign-event-handlers-to-controls-on-user-form-created-dynamically-in-vba/10596866#10596866. That approach sets an object equal to the newly added control. You then have access to its properties at compile time. The advice by @DavidGM should work, so please [edit] your question with what you tried and an explanation of `not worked`. – Byron Wall Jul 10 '15 at 16:11
  • Also `it gives an error` is not an adequate description. Please provide the exact error. I suspect it was a compile time error because that property does not exist at compile time since the compiler does not know you are creating the object. Instead of letting us assume that, please [edit] those details into your question. – Byron Wall Jul 10 '15 at 16:13
  • Thanks for the warning , I edited the question like you said. – Ozgur G. Jul 13 '15 at 06:51
  • What happens when you use the syntax suggested by @DavidGM? You should be able to go through `UserForm1.Controls("object name")`. Did you try the other approach in the link I provided? That is create an object and `Set` when you add it. – Byron Wall Jul 13 '15 at 14:39
  • 1
    When I use @DavidGM's syntax I'm encountering Method or Data member not found error. I work it out with the link you send. Thanks for the help guys.I appreciate it. – Ozgur G. Jul 14 '15 at 12:39

0 Answers0