June 26, 2016
Hi everyone, my name is Maurizio and my problem is this:
In a userform I inserted a Textbox and a CommandButton
Now my question is this:
Would it be possible to create more (Textbox) (Dynamics) just by pressing the (Button) even just five or six times obtaining the same dimensions as the first one?
I managed to create the first textbox (Dynamic), it would be enough for me to be able to create all the others at each (Press) of the button.
Thanks to everyone who wants to help me out.
Greetings from Maurizio
February 20, 2020
Hello,
An example that you can use and adjust to your needs
Private ctrlx As Collection
Private Sub AddNewControls_Textbox()
Dim i As Long
Dim add_new_ctrl As Control
On Error Resume Next
For i = 1 To 6 ' to add 6 new textbox
Set add_new_ctrl = Me.Controls.Add("Forms.TextBox.1", "NewTextbox" & i) 'you can change the name "NewTexbox"
' with the same size as the existing textbox
add_new_ctrl.Top = Me.TextBox1.Top + i * 20
add_new_ctrl.Left = Me.TextBox1.Left
add_new_ctrl.Width = Me.TextBox1.Width
add_new_ctrl.Height = Me.TextBox1.Height
add_new_ctrl.Text = "Hello World..."
ctrlx.Add add_new_ctrl, add_new_ctrl.Name
Next
If Not add_new_ctrl Is Nothing Then Set add_new_ctrl = Nothing
End Sub
Private Sub UserForm_Initialize()
Set ctrlx = New Collection
End Sub
Miguel
June 26, 2016
Hi Miguel Santos and Hi also to Philip Treacy:
Haora: Miguel I like your listing even though to tell the truth I had already done something like this a few years ago for another database project.
But perhaps Google has not translated my request well since I asked that the Text (Dynamics) not be created all at once; But on pressing the key
Surely in this case you should do a (For x 1 to 5 - Next) only that I don't know how to write the whole thing
And this is where the difficulty lies?
Thanks anyway for your interest
Do I fully support Philip's question?
Hello everyone and good luck
By A.Maurizio
Trusted Members
Moderators
November 1, 2018
June 26, 2016
Hi Velouria
Yes the intent was this:
But I have reached a point in my project; Also following your advice and help
That would also be fine.
The only thing that I would still like to obtain would be to have a progressive number from 1 to 6 that would gradually increase by just pressing the button
Example :
Now I have to put the number by hand in the Text box2
While I wish it was automatic
Take a look at my latest job.
And let me know if it's feasible; And what do you think?
Thank you
February 20, 2020
Hello, yes Philip,
A.Maurizio, I'll take a look at your workbook
You want something like that?
Option Explicit
Private ctrlx As Collection
Private Sub AddNewControls_Textbox()
Dim add_new_ctrl As Control
Dim ctrl As MSForms.Control
Dim x As Integer
x = 0
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then
x = x + 1
End If
Next ctrl
Debug.Print x
If x >= 6 Then
MsgBox "Last textbox has already been added!"
GoTo ExitSub:
End If
Set add_new_ctrl = Me.Controls.Add("Forms.TextBox.1", "NewTextbox" & x + 1) 'you can change the name "NewTexbox"
' with the same size as the existing textbox
add_new_ctrl.Top = Me.TextBox1.Top + x * 20
add_new_ctrl.Left = Me.TextBox1.Left
add_new_ctrl.Width = Me.TextBox1.Width
add_new_ctrl.Height = Me.TextBox1.Height
add_new_ctrl.Text = "Hello World..."
If Control_Exists("NewTextbox" & x, Me) = True Then
'Cancel is true
Else
ctrlx.Add add_new_ctrl, add_new_ctrl.Name
End If
ExitSub:
If Not add_new_ctrl Is Nothing Then Set add_new_ctrl = Nothing
Exit Sub
End Sub
Public Function Control_Exists(ctl_Name As String, ByRef MyForm As UserForm) As Boolean
Dim ctrl As Control
On Error GoTo ErrHandler
For Each ctrl In MyForm.Controls
If ctrl.Name = ctl_Name Then
Control_Exists = True
Exit For
End If
Next ctrl
ExitFunction:
Exit Function
ErrHandler:
Debug.Print Err, Err.Description
Resume ExitFunction
End Function
Private Sub UserForm_Initialize()
Set ctrlx = New Collection
End Sub
Private Sub CommandButton1_Click()
AddNewControls_Textbox
End Sub
Miguel
June 26, 2016
Hi Miguel
You are uin Grande
what you did was just what I wanted to do from the beginning
Thanks Infinite
And thanks also to everyone else
Now I just have to try to give it a better coordinate to all the textboxes and then the game is done.
Now I want to reveal the secret of my request.
You know the Fatitic Bar of the operating system objects (Windows-Vista) the one where all the (Gadgets) were
Among these was one that has always intrigued me: And it was BlookNotes
Where you had the option of Writing in multiple sheets and all had their place in the memory of (Windows)
Well I want to create this
All the rest I had already created in other programs including dynamic (TextBox).
But never done this way here!
(P.S) I had also made that classic Analog Clock with six melts of different dials; Including the Seven-Sided Digital one and the Weather.
But all this and all not another story.
Again Thanks to all of you and especially to you who kindly offered me this wonderful Project.
June 26, 2016
Thanks Infinite Miguel
You are a friend
Greetings and Happy Weekend
(p.S) Listen to Miguel
I would like to ask you this again:
If you had to follow your project
How do I go about Identifying the (First Textbox) Rather than the (Second and so on?
I'll explain:
When I did a database program with dynamic Label and textbox many years ago
Nor did I declare their quantity and value through a series of cells selected on sheet 1
Then I gave their Voices once the form opened and the database started from there; With all the data that was saved in a Document (Txt)
Thereforep when I recalled everything from (Txt) to (Textbox) the data already knew where to go as there were precise coordinates.
Therefore in your Case: How do I identify one of these 5 (Textbox) with your method?
Thanks right now for all the tips you want to give me about it Hello
February 20, 2020
Hello,
A.Maurizio, I don't know if this is what you asked me, but I leave here 3 examples
First example: Update to add new textbox
Private Sub AddNewControls_Textbox()
Dim add_new_ctrl As Control
Dim ctrl As MSForms.Control
Dim x As Integer
x = 0
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then
x = x + 1
End If
Next ctrl
Debug.Print x
If x = 0 Then
MsgBox "Error: Textbox1 does not exist!"
Exit Sub
End If
If x >= 6 Then
MsgBox "Last textbox has already been added!"
Exit Sub
End If
If Control_Exists("NewTextbox" & x, Me) = True Then
MsgBox "Error: Textbox Already exists!"
Exit Sub
End If
Set add_new_ctrl = Me.Controls.Add("Forms.TextBox.1", "NewTextbox" & x) 'you can change the name "NewTexbox"
' with the same size as the existing textbox
add_new_ctrl.Top = Me.TextBox1.Top + x * 20 ' 20 is textbox.height(in this example is 18) = (18+2)
add_new_ctrl.Left = Me.TextBox1.Left
add_new_ctrl.Width = Me.TextBox1.Width
add_new_ctrl.Height = Me.TextBox1.Height
Select Case x
Case 1
add_new_ctrl.ForeColor = vbRed
add_new_ctrl.Font.Size = 10
add_new_ctrl.Text = "This Textbox name: " & add_new_ctrl.Name
'--------------------------------
'you can add more cod here if you want
'--------------------------------
Case 2
add_new_ctrl.ForeColor = vbBlack
add_new_ctrl.Font.Size = 12
add_new_ctrl.Text = "This Textbox name: " & add_new_ctrl.Name
'--------------------------------
'you can add more cod here if you want
'--------------------------------
Case 3
add_new_ctrl.ForeColor = vbGreen
add_new_ctrl.Font.Size = 14
add_new_ctrl.Text = "This Textbox name: " & add_new_ctrl.Name
'--------------------------------
'you can add more cod here if you want
'--------------------------------
Case 4
add_new_ctrl.ForeColor = vbBlue
add_new_ctrl.Font.Size = 8
add_new_ctrl.Text = "This Textbox name: " & add_new_ctrl.Name
'--------------------------------
'you can add more cod here if you want
'--------------------------------
Case 5
add_new_ctrl.ForeColor = vbYellow
add_new_ctrl.Font.Size = 10
add_new_ctrl.Text = "This Textbox name: " & add_new_ctrl.Name
'--------------------------------
'you can add more cod here if you want
'--------------------------------
Case Else
'--------------------------------
'add some cod here if you want
'--------------------------------
End Select
ctrlx.Add add_new_ctrl, add_new_ctrl.Name
If Not add_new_ctrl Is Nothing Then Set add_new_ctrl = Nothing
End Sub
Second example: assuming you want to save the data for each textbox on the excel sheet
with the function to determine if control(textbox) exists
Private Sub CommandButton2_Click()
Dim ctrl As MSForms.TextBox
Dim i As Integer
With Application.ThisWorkbook.Worksheets("Folha1")
.Select
.Range("A1").Value = Me.TextBox1.Text
For i = 1 To 5 ' only for the 5 new textbox
On Error Resume Next
Set ctrl = Me.Controls("NewTextbox" & i)
On Error GoTo 0
If Control_Exists("NewTextbox" & 1, Me) = True Then
If ctrl.Name = "NewTextbox1" Then
.Range("A2").Value = ctrl.Text
End If
Else
.Range("A2").Value = "No data!"
End If
If Control_Exists("NewTextbox" & 2, Me) = True Then
If ctrl.Name = "NewTextbox2" Then
.Range("A3").Value = ctrl.Text
End If
Else
.Range("A3").Value = "No data!"
End If
If Control_Exists("NewTextbox" & 3, Me) = True Then
If ctrl.Name = "NewTextbox3" Then
.Range("A4").Value = ctrl.Text
End If
Else
.Range("A4").Value = "No data!"
End If
If Control_Exists("NewTextbox" & 4, Me) = True Then
If ctrl.Name = "NewTextbox4" Then
.Range("A5").Value = ctrl.Text
End If
Else
.Range("A5").Value = "No data!"
End If
If Control_Exists("NewTextbox" & 5, Me) = True Then
If ctrl.Name = "NewTextbox5" Then
.Range("A6").Value = ctrl.Text
End If
Else
.Range("A6").Value = "No data!"
End If
Next i
End With
If Not ctrl Is Nothing Then Set ctrl = Nothing
End Sub
third example: to get the control index number
you can use the index number of the control to use in macros
Private Sub CommandButton3_Click()
Dim ctrl As MSForms.Control
Dim x As String
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then
x = x & ctrl.Name & " - " & ctrl.TabIndex & vbCrLf
End If
Next ctrl
MsgBox x
End Sub
Miguel
1 Guest(s)