ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Please HLEP: whats wrong with my code!? Adding textbox as runtime (https://www.excelbanter.com/excel-programming/439001-please-hlep-whats-wrong-my-code-adding-textbox-runtime.html)

Sam

Please HLEP: whats wrong with my code!? Adding textbox as runtime
 
Hi All,

I want to add textboxes at runtime based on what user inputs in a textbo.

For eg:

I have a textbox "Total Students" , so Once I input a number in that
textbox, I want to have that many textboxes on the form during run time. If
I input 4 I want four textboxes to be displayed on the form.

Here is my code that I have so far.

Private Sub TextBox1_AfterUpdate()
Dim cCntrl As Control, Dim i as integer
i = 1

For i = 1 To 10

If Me.TextBox1.Value = i Then

With Frame14

Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox1", True)
With cCntrl
.Width = 150
.Height = 25
.Top = 10
.Left = 10
.ZOrder (0)
End With

End With

End If

Next i

End Sub


Thanks in advance

Per Jessen[_2_]

Please HLEP: whats wrong with my code!? Adding textbox as runtime
 
Hi Sam

Try this:

Private Sub TextBox1_AfterUpdate()
Dim cCntrl As Control, Dim i as integer
If Me.TextBox1.Value =1 and me.textbox1.value<=10 Then

With Frame14
for 1=1 to me.textbox1.value

Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox1",
True)
With cCntrl
.Width = 150
.Height = 25
.Top = 10
.Left = 10
.ZOrder (0)
End With
next
End With
End If
Next i
End Sub

Regards,
Per

On 29 Jan., 22:05, sam wrote:
Hi All,

I want to add textboxes at runtime based on what user inputs in a textbo.

For eg:

I have a textbox "Total Students" , so Once I input a number in that
textbox, I want to have that many textboxes on the form during run time. If
I input 4 I want four textboxes to be displayed on the form.

Here is my code that I have so far.

Private Sub TextBox1_AfterUpdate()
Dim cCntrl As Control, Dim i as integer
i = 1

For i = 1 To 10

* * If Me.TextBox1.Value = i Then

* * * * With Frame14

* * Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox1", True)
* * With cCntrl
* * * * .Width = 150
* * * * .Height = 25
* * * * .Top = 10
* * * * .Left = 10
* * * * .ZOrder (0)
* * End With

End With

End If

Next i

End Sub

Thanks in advance



John

Please HLEP: whats wrong with my code!? Adding textbox as runtime
 
Sam,
I am no expert adding controls on the fly and have quickly, as I am about to
head off home, cobbled the following code together which should do roughly
what you want or if not, may give you some further pointers.

You will though, need to develop method to collect any data entered in each
textbox by user.

Hope useful

Private Sub CommandButton1_Click()
Dim NewTextBox As MSForms.TextBox
Dim nostudents As Integer

On Error Resume Next
nostudents = Me.TotalStudents.Text


'Create Text Boxes
For X = 0 To nostudents - 1

Set NewTextBox = Me.Controls.Add("Forms.textbox.1")

With NewTextBox
.Name = "MyTextBox" & X + 1
.Top = 20 + (22 * X)
.Left = 10
.Width = 100
.Height = 18
.Font.Size = 7
.Font.Name = "Tahoma"
.BorderStyle = fmBorderStyleSingle
.SpecialEffect = fmSpecialEffectSunken

End With

Next

End Sub
--
jb


"sam" wrote:

Hi All,

I want to add textboxes at runtime based on what user inputs in a textbo.

For eg:

I have a textbox "Total Students" , so Once I input a number in that
textbox, I want to have that many textboxes on the form during run time. If
I input 4 I want four textboxes to be displayed on the form.

Here is my code that I have so far.

Private Sub TextBox1_AfterUpdate()
Dim cCntrl As Control, Dim i as integer
i = 1

For i = 1 To 10

If Me.TextBox1.Value = i Then

With Frame14

Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox1", True)
With cCntrl
.Width = 150
.Height = 25
.Top = 10
.Left = 10
.ZOrder (0)
End With

End With

End If

Next i

End Sub


Thanks in advance


Per Jessen[_2_]

Please HLEP: whats wrong with my code!? Adding textbox as runtime
 
Sam,

Actually, I think I would create all 10 text boxes at design state,
then hide all 10 when the userform is initialized. Then you just have
to unhide the choosen number of textboxes....

Hopes this helps.
....
Per


On 29 Jan., 22:17, Per Jessen wrote:
Hi Sam

Try this:
just h
Private Sub TextBox1_AfterUpdate()
Dim cCntrl As Control, Dim i as integer
If Me.TextBox1.Value =1 and me.textbox1.value<=10 *Then

With Frame14
for 1=1 to me.textbox1.value

* * Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox1",
True)
* * With cCntrl
* * * * .Width = 150
* * * * .Height = 25
* * * * .Top = 10
* * * * .Left = 10
* * * * .ZOrder (0)
* * End With
next
End With
End If
Next i
End Sub

Regards,
Per

On 29 Jan., 22:05, sam wrote:



Hi All,


I want to add textboxes at runtime based on what user inputs in a textbo.


For eg:


I have a textbox "Total Students" , so Once I input a number in that
textbox, I want to have that many textboxes on the form during run time.. If
I input 4 I want four textboxes to be displayed on the form.


Here is my code that I have so far.


Private Sub TextBox1_AfterUpdate()
Dim cCntrl As Control, Dim i as integer
i = 1


For i = 1 To 10


* * If Me.TextBox1.Value = i Then


* * * * With Frame14


* * Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox1", True)
* * With cCntrl
* * * * .Width = 150
* * * * .Height = 25
* * * * .Top = 10
* * * * .Left = 10
* * * * .ZOrder (0)
* * End With


End With


End If


Next i


End Sub


Thanks in advance- Skjul tekst i anførselstegn -


- Vis tekst i anførselstegn -




All times are GMT +1. The time now is 02:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com