PDA

Click to See Complete Forum and Search --> : Dynamically create Textboxes & Checkboxes in User Forms


silvrwood
Jun 10th, 2005, 04:36 PM
I have created on user form. Within it is a dropdown that will prompt for a number. That integer saves to a variable which is public to the module.
I have created a second user form. Within that user form I would like to dynamically create a number of textboxes based on the integer to which the aforementioned dropdown saved its value. Is there a method to create textboxes on the fly within user forms, or how would I make this happen?

salvelinus
Jun 10th, 2005, 05:04 PM
You can create them dynamically, but have to set a lot of properties (top, left, etc), which could be a lot of work if the user has a lot of choices.

Dim t as textbox
t.top = 1600
t.name = "txtFoo"
'and so on

or, you could create the controls you want, set visible = false, and set visible = true based on whatever the value is.
Or, if the controls are the same but their values differ, just set those values when the form opens.

silvrwood
Jun 13th, 2005, 12:50 PM
Do I have to set all of the properties, or just the ones like location that would be specific to the control? Would the rest take defaults? And from looking at your response, am I correct in figuring then that all I have to do is declare a variable of that type? There isn't an add method to tell it that variable goes into a specific user form? How do I associate this with a specific userform if I want to get an answer from a dropdown in one form, which would show the input userform upon clicking in the aforementioned dropdown?

kfcSmitty
Jun 13th, 2005, 01:10 PM
this should help you

http://www.vbforums.com/showthread.php?t=115007&highlight=creating+textboxes+runtime

silvrwood
Jun 14th, 2005, 03:44 PM
I'm too much of a newbie at VBA to quite follow that one. I'm not sure where in my code I would be adding the text box and how I would be telling it to which user form the text box is being added.

For the time being, I think it will be more efficient for me to go with your first suggestion and simply make the text boxes all invisible then use an iterative structure to change them to visible until the number the dropdown supplied is reached. However, that brings more questions up for me.

I tried accessing the text boxes like so:

Dim BxName As String
Dim ChkName As String
Dim i As Integer
'Num is a global integer

For i = 1 To Num

BxName = "NameBox" & CStr(Num)
ChkName = "U18Chk" & CStr(Num)

KidPrompt.BxName.Visible = True
KidPrompt.ChkName.Visible = True

Next Num

But it doesn't let me replace the textbox name with a variable holding that name. How can I loop through the text & check boxes to make them visible so that it stops after the global Num value & only makes visible certain boxes (there are others that will be made visible if the checkbox is true)

kaaja
Jul 15th, 2005, 01:19 AM
this should help you

http://www.vbforums.com/showthread.php?t=115007&highlight=creating+textboxes+runtime

This will not work, there is no index in texbox property in VBA.


Kaaja