Automatically add a textbox to a user form based on user requireme

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was wondering if it is possible to automatically add a text box to a user
form based on user requirements?

I'm trying to make a wizard, and the way i want it to work is:

1. A userform will appear asking how many sources of revenue the user would
like to enter.

2. On the next userform i want the appropriate number of text boxes to
appear so that the user can enter all of their data. For example. Say a user
enters that they would want to enter 3 sources of revenue. On the next screen
i want a user form to appear with three textboxes so that they can enter all
of their data.

Is there a way to do this?

thanks in advance for your help!
 
Hi Brite,

populate the second userform with as many textboxes
as a value from the first userform returns:

Private Sub UserForm_Click()

Dim l As Long
UserForm1.Hide
For l = 1 To Me.TextBox1.Value
Set obj = UserForm2.Controls.Add("Forms.TextBox.1")
If l = 1 Then
obj.Top = 20
Else
obj.Top = l * 20
End If
Next
UserForm2.Show
End Sub


Adjusting the size of the userform2 is another matter,
and controlling where and in what size the textboxes shall appear.

Just to show that it is possible.
There is a lot more to it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
Here's one way to do it. Paste this sub routine in your form code module:

Private Sub AddTextBox(iCount As Integer)
Dim i As Integer
For i = 1 To iCount
Me.Controls.Add "Forms.TextBox.1", "txtBox" & i, True
'adjust the textbox position as needed
With Me.Controls("txtBox" & i)
'10 pixes from the left edge of the form
.Left = 10
'20 pixes from the top of the form,
'2 pixels between each text box
.Top = (i * (.Height + 2)) + 20
End With

Next i
End Sub

Call it in your form by:

AddTextBox 3
 
I prefer putting my controls on the userform during design time, so I know
how the form will look. I keep certain ones hidden, then show them (rather
than drawing new ones) when needed.

- Jon
 

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

Back
Top