aliend8k
11-27-2017, 01:51 PM
Hello again -
I've been working on a userform to have a command button dynamically ADD an empty text-box to the form. I want to button to ADD a new empty text box below the prior box. I'm at wits end trying to run this as a FOR LOOP where there is an undefined number of iterations (since this is going to be used for a travel itinerary type input). The user would click the command button to "add" a new empty line. I'm only able to get it to work using the FOR loop if I define the entire loop (I= 1 to x = where x is a defined number). I'm trying to change this so that the button will use x as the iteration - adding both the iteration to the textbox name - and spacing considerations...
The following code works - by adding 4 evenly spaced empty text boxes (i=5 in the end). any ideas how to run a command button within a loop to execute each iteration one at a time? Thanks! This of course would all be used to later post the values of the textbox...
Private Sub CommandButton1_Click()
Dim i As Integer
'Dim n As Integer 'tr
'n = n + 1 'tried using this in place of "for i = 1 to n - did not work"
Dim txtbox As Control
For i = 1 To 4
' If TypeName(txtbox) = "TextBox" Then
Set txtbox = UserForm1.Controls.Add("Forms.TextBox.1")
With txtbox
.Name = "checkName" & i
.Left = 10
.Height = 20
.Top = 20 + (30 * i)
End With
Next i
End Sub
I've been working on a userform to have a command button dynamically ADD an empty text-box to the form. I want to button to ADD a new empty text box below the prior box. I'm at wits end trying to run this as a FOR LOOP where there is an undefined number of iterations (since this is going to be used for a travel itinerary type input). The user would click the command button to "add" a new empty line. I'm only able to get it to work using the FOR loop if I define the entire loop (I= 1 to x = where x is a defined number). I'm trying to change this so that the button will use x as the iteration - adding both the iteration to the textbox name - and spacing considerations...
The following code works - by adding 4 evenly spaced empty text boxes (i=5 in the end). any ideas how to run a command button within a loop to execute each iteration one at a time? Thanks! This of course would all be used to later post the values of the textbox...
Private Sub CommandButton1_Click()
Dim i As Integer
'Dim n As Integer 'tr
'n = n + 1 'tried using this in place of "for i = 1 to n - did not work"
Dim txtbox As Control
For i = 1 To 4
' If TypeName(txtbox) = "TextBox" Then
Set txtbox = UserForm1.Controls.Add("Forms.TextBox.1")
With txtbox
.Name = "checkName" & i
.Left = 10
.Height = 20
.Top = 20 + (30 * i)
End With
Next i
End Sub