I've been using VBA for about two years, but I've just started using dynamic controls today.
I know how to add a dynamic control to a form at a position relative to an existing contol, i.e.,
<code>
Private Sub CommandButton1_Click()
Set TextBox1 = UserForm1.Controls.Add("Forms.TextBox.1", "Test", True)
With TextBox1
.Text = "Test TextBox"
.FontSize = 8
.Top = cmdTest.top + </code><code>cmdTest.height + 30</code>
<code> .Width = 150
.Left = 12
.Height = 18
End With
End Sub
</code>I need to know:
(1) How can I use the same command button to add multiple textboxes to the form, with their positions relative to the last control added i.e. the form loads with only a command button. The user clicks that button and a textbox is added. The user clicks the same button and another textbox is added below the first one.
(2) How can I reference the controls that were dynamically added, i.e. I need a routine to get the sum of the values in each dynamically added textbox. There could be one textbox or 10, depending on how many times the user has added a textbox. For controls added at design time, I'd just have, for example
<code>iTotal = textbox1.text + textbox2.text + textbox3.text....</code>
I don't know how to do this with dynamic control where the number of controls added can vary.
I hope this is clear, and thanks in advance for your help
I know how to add a dynamic control to a form at a position relative to an existing contol, i.e.,
<code>
Private Sub CommandButton1_Click()
Set TextBox1 = UserForm1.Controls.Add("Forms.TextBox.1", "Test", True)
With TextBox1
.Text = "Test TextBox"
.FontSize = 8
.Top = cmdTest.top + </code><code>cmdTest.height + 30</code>
<code> .Width = 150
.Left = 12
.Height = 18
End With
End Sub
</code>I need to know:
(1) How can I use the same command button to add multiple textboxes to the form, with their positions relative to the last control added i.e. the form loads with only a command button. The user clicks that button and a textbox is added. The user clicks the same button and another textbox is added below the first one.
(2) How can I reference the controls that were dynamically added, i.e. I need a routine to get the sum of the values in each dynamically added textbox. There could be one textbox or 10, depending on how many times the user has added a textbox. For controls added at design time, I'd just have, for example
<code>iTotal = textbox1.text + textbox2.text + textbox3.text....</code>
I don't know how to do this with dynamic control where the number of controls added can vary.
I hope this is clear, and thanks in advance for your help