Accesing Controls in Excel VBA Userform - vba

I am creating form controls in a custom userform and have having difficulty in accesing them after creation. The amount of ComboBoxes and TextBoxes depends on user input. I am using a CommandButton to figure the correct syntax, but am at a loss to find the control. I have used several different naming conventions inside the CommandButton_Click method and nothing works.
My code for the userform to create my controls is as follows:
Sub createDetails()
Dim details As Variant
details = TextBox3.Value
remainTot = TextBox2.Value
If TextBox3.Value = "" Or TextBox3.Value = 0 Then
MsgBox "Must have at least 1 detail"
Exit Sub
Else
End If
For i = 1 To details
n = i - 1
Dim SubPay As Control
Dim CatPay As Control
Dim AmtPay As Control
Set theLbl = frmInvoice.Controls.Add("Forms.Label.1", "lbl_" & i, True)
With theLbl
.Caption = "Detail " & i
.Left = 20
.Width = 60
.Top = n * 24 + 110
.Font.Size = 10
End With
Set SubPay = frmInvoice.Controls.Add("Forms.ComboBox.1", "SubComboBox_" & i, True)
With SubPay
.Top = 108 + (n * 24)
.Left = 60
.Height = 18
.Width = 100
.Name = "subBox" & i
.Font.Size = 10
.TabIndex = n * 3 + 6
.TabStop = True
.RowSource = "PayeeList"
End With
Set CatPay = frmInvoice.Controls.Add("Forms.ComboBox.1", "CatComboBox_" & i, True)
With CatPay
.Top = 108 + (n * 24)
.Left = 165
.Height = 18
.Width = 100
.Name = "catBox" & i
.Font.Size = 10
.TabIndex = n * 3 + 7
.TabStop = True
.RowSource = "CatList"
End With
Set AmtPay = frmInvoice.Controls.Add("Forms.TextBox.1", "AmtTextBox" & i, True)
With AmtPay
.Top = 108 + (n * 24)
.Left = 270
.Height = 18
.Width = 50
.Name = "amtBox" & i
.Font.Size = 10
.TabIndex = n * 3 + 8
.TabStop = True
End With
Next i
Dim TBox As Control
Set TBox = frmInvoice.Controls.Add("Forms.TextBox.1", "TotalLbl", True)
With TBox
.Top = 130 + ((details - 1) * 24)
.Left = 270
.Height = 18
.Width = 50
.Name = "totBox"
.Font.Size = 10
'.TabIndex = (details - 1) * 3 + 9
.TabStop = False
.Value = TextBox2.Value
End With
Set theLbl = frmInvoice.Controls.Add("Forms.Label.1", "totLbl", True)
With theLbl
.Caption = "Total"
.Left = 225
.Width = 40
.Top = 135 + ((details - 1) * 24)
.Font.Size = 10
End With
frmInvoice.Height = 200 + details * 24
With CommandButton1
.Top = 150 + details * 24
.TabStop = True
.TabIndex = (details - 1) * 3 + 9
End With
With CommandButton2
.Top = 150 + details * 24
.TabStop = False
'.TabIndex = (details - 1) * 3 + 10
End With
End Sub
The code for the CommndButton which doesn't work is:
Private Sub CommandButton1_Click()
frmInvoice.Controls("amtBox1").Value = 1
frmInvoice.Controls(amtBox1).Value = 2
frmInvoice.Controls(AmtTextBox1).Value = 3
frmInvoice.Controls("AmtTextBox1").Value = 4
End Sub
Any help is greatly appreciated.
Screen shot of my userform:

Try using
frmInvoice.Controls("amtBox1").Text
instead of
frmInvoice.Controls("amtBox1").Value

I think your error because of
Private Sub CommandButton1_Click()
frmInvoice.Controls("amtBox1").Value = 1 'is true
frmInvoice.Controls(amtBox1).Value = 2 'is false because there isn't double quotes
frmInvoice.Controls(AmtTextBox1).Value = 3 'is false because there isn't dobule quotes
frmInvoice.Controls("AmtTextBox1").Value = 4 'is false the AmtTextBoxt1 name is changed "amtBox1"
End Sub
Maby, Do you want to this?
Private Sub CommandButton1_Click()
Dim i As Integer
Dim total As Currency
With frmInvoice
For i = 1 To TextBox3.Value
total = total + .Controls("amtBox" & i).Value
Next i
.Controls("totBox").Text = Format(total, "####.00")
End With
End Sub

Related

Programmatically add and delete Combobox, Textbox and Checkbox to Userform - VBA

I am creating a userform which has 1 combobox (1), 2 textboxes (2 and 3), and 11 checkboxes (A-K) as in picture. The first instance of all controls will be present, but when a user clicks on Add Class, a new set of all controls has to be created and when Remove Class should delete a particular row.
I have managed to get the requirement for one instance, but I could not figure out how do I do it for N times, with the size of the userform to be expanding also.
Private Sub cmdAddClass()
Dim cCheckBox As Control, r As Long, r1 As Range
Set r1 = Sheets("Sheet1").Range("A2:A12")
Set cComboBox = Me.Controls.Add("Forms.ComboBox.1")
With cComboBox
.Height=25.5
.Width=102
.Top=50
.Left =6
End With
Set cTextBox = Me.Controls.Add("Forms.TextBox.1")
With cTextBox
.Height=25.5
.Width=54
.Top=50
.Left =114
End With
For r = 1 To WorksheetFunction.CountA(r1)
If r1(r) <> vbNullString Then
Set cCheckBox = Me.Controls.Add("Forms.CheckBox.1", "Checkbox" & r, True)
With cCheckBox
.Width = 21
.Height = 11
.Top = 50
If r = 1 Then
.Left = 270
ElseIf r = 2 Then
.Left = 348
ElseIf r = 3 Then
.Left = 420
ElseIf r = 4 Then
.Left = 492
ElseIf r = 5 Then
.Left = 564
ElseIf r = 6 Then
.Left = 636
ElseIf r = 7 Then
.Left = 701.95
ElseIf r = 8 Then
.Left = 780
ElseIf r = 9 Then
.Left = 876
ElseIf r = 10 Then
.Left = 966
Else
.Left = 1050
End If
End With
End If
Next r
End Sub
Here is an example you can adapt.
Private Sub btnAddClass_Click()
Dim ctrl As Control, newCtrl As Control, offsetTop As Integer
offsetTop = 30
For Each ctrl In Me.Controls
If TypeName(ctrl) <> "CommandButton" Then
If ctrl.Top = btnAddClass.Top - offsetTop Then
If TypeName(ctrl) = "ComboBox" Then
Set newCtrl = Me.Controls.Add("Forms.ComboBox.1")
ElseIf TypeName(ctrl) = "TextBox" Then
Set newCtrl = Me.Controls.Add("Forms.TextBox.1")
ElseIf TypeName(ctrl) = "CheckBox" Then
Set newCtrl = Me.Controls.Add("Forms.Checkbox.1")
End If
With newCtrl
.Height = ctrl.Height
.Width = ctrl.Width
.Top = ctrl.Top + offsetTop
.Left = ctrl.Left
End With
End If
End If
Next ctrl
btnAddClass.Top = btnAddClass.Top + offsetTop
btnRemoveClass.Top = btnRemoveClass.Top + offsetTop
Me.Height = Me.Height + offsetTop
End Sub
Private Sub btnRemoveClass_Click()
Dim ctrl As Control, offsetTop As Integer
offsetTop = 30
For Each ctrl In Me.Controls
If TypeName(ctrl) <> "CommandButton" Then
If ctrl.Top = btnAddClass.Top - offsetTop Then
Me.Controls.Remove (ctrl.Name)
End If
End If
Next ctrl
btnAddClass.Top = btnAddClass.Top - offsetTop
btnRemoveClass.Top = btnRemoveClass.Top - offsetTop
End Sub
Notes:
To get this to work I need to explain the set-up:
Your initial row of controls have a Top property set to 12
There are two buttons named btnAddClass and btnRemoveClass with Top set to 42
The offset in height between the controls and the buttons is 30.
To add new controls you simply loop over each control, create a new one, and set its Top to be the existing control Top value + the offset (i.e. 30). At the same time you shift the buttons down by 30 and increase the userform height by 30.
To remove controls you get the last row by checking the Top property relative to the btnAddClass. You then delete those controls and shift the buttons up by 30 and decrease the userform height by 30.

Extracting data from a dynamic userform VBA

All,
I have the below code which creates a dynamic userform based on a list located in an excel worksheet. (Please see picture below)
When the user selects submit I would like to extract all the answers from the user form into an excel file.
Does anyone know how I would do this as I have hit a brick wall in thoughts, the user form to my knowledge has to be built via vba as the list of Project ID & UR can vary from 1 line to thousands of lines.
Any help would be much appreciated.
Sub addLabel()
UserForm6.Show vbModeless
Dim theLabel As Object
Dim ComboBox1 As Object
Dim CommandApp As Object
Dim CommandCan As Object
Dim buttonheight As Long
Dim labelCounter As Long
For Each c In Sheets("Sheet1").Range("A1:A100")
If c.Value = "" Then Exit For
Set theLabel = UserForm6.Controls.Add("Forms.label.1", "Test" & c, True)
With theLabel
.Caption = c
.Left = 10
.Width = 50
.Height = 20
.Font.Size = 10
If c.Row = 1 Then
.Top = 34
Else
.Top = 25 + (20 * (c.Row - 1)) + 9
End If
End With
Set ComboBox1 = UserForm6.Controls.Add("Forms.combobox.1", "Test" & c, True)
With ComboBox1
.AddItem "Approved"
.AddItem "Partially Approved"
.AddItem "Not Approved"
.Left = 190
.Width = 120
.Height = 20
.Font.Size = 10
If c.Row = 1 Then
.Top = 30
Else
.Top = 30 + (20 * (c.Row - 1))
buttonheight = 30 + (20 * (c.Row - 1))
End If
End With
Next c
For Each c In Sheets("Sheet1").Range("B1:B100")
If c.Value = "" Then Exit For
Set theLabel = UserForm6.Controls.Add("Forms.label.1", "Test" & c, True)
With theLabel
.Caption = c
.Left = 90
.Width = 70
.Height = 20
.Font.Size = 10
If c.Row = 1 Then
.Top = 34
Else
.Top = 25 + (20 * (c.Row - 1)) + 9
End If
End With
Next c
With UserForm6
.Width = 340
.Height = buttonheight + 90
End With
Set CommandApp = UserForm6.Controls.Add("Forms.Commandbutton.1", "Test" & c, True)
With CommandApp
.Caption = "Submit"
.Left = 10
.Width = 140
.Font.Size = 10
.Top = buttonheight + 30
End With
Set CommandCan = UserForm6.Controls.Add("Forms.Commandbutton.1", "Test" & c, True)
With CommandCan
.Caption = "Cancel"
.Left = 170
.Width = 140
.Font.Size = 10
.Top = buttonheight + 30
End With
End Sub
You will need create variables to hold references to the newly created CommandButtons. By adding the WithEvents modifier you will be able to receive the CommandButton events.
Naming the controls after cell values is problematic. A better solution is to use the MSForms Control Tag property to hold your references. In my example below I add a qualified reference to the target cell.
Changed the subroutines name from addLabel to something more meaningful Show_UserForm6.
Combobox values as they are added.
Userform6 Module
Option Explicit
Public WithEvents CommandApp As MSForms.CommandButton
Public WithEvents CommandCan As MSForms.CommandButton
Private Sub CommandApp_Click()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "ComboBox" Then
Range(ctrl.Tag).Value = ctrl.Value
End If
Next
End Sub
Private Sub CommandCan_Click()
Unload Me
End Sub
Refactored Code
Sub Show_UserForm6()
Const PaddingTop = 34, Left1 = 10, Left2 = 90, Left3 = 190
Dim c As Range
Dim Top As Single
Top = 34
With UserForm6
.Show vbModeless
For Each c In Sheets("Sheet1").Range("A1:A100")
If c.Value = "" Then Exit For
With getNewControl(.Controls, "Forms.Label.1", Left1, 50, 20, Top)
.Caption = c.Value
.Tag = "'" & c.Parent.Name & "'!" & c.Address
End With
With getNewControl(.Controls, "Forms.Label.1", Left2, 50, 20, Top)
.Caption = c.Offset(0, 1).Value
.Tag = "'" & c.Parent.Name & "'!" & c.Offset(0, 2).Address
End With
With getNewControl(.Controls, "Forms.ComboBox.1", Left3, 120, 20, Top)
.List = Array("Approved", "Partially Approved", "Not Approved")
.Tag = "'" & c.Parent.Name & "'!" & c.Offset(0, 2).Address
.Value = c.Offset(0, 2).Value
End With
Top = Top + 20
Next
Set .CommandApp = getNewControl(.Controls, "Forms.Commandbutton.1", 10, 140, 20, Top + 10)
With .CommandApp
.Caption = "Submit"
End With
Set .CommandCan = getNewControl(.Controls, "Forms.Commandbutton.1", 170, 140, 20, Top + 10)
With .CommandCan
.Caption = "Cancel"
End With
End With
End Sub
Function getNewControl(Controls As MSForms.Controls, ProgID As String, Left As Single, Width As Single, Height As Single, Top As Single) As MSForms.Control
Dim ctrl As MSForms.Control
Set ctrl = Controls.Add(ProgID)
With ctrl
.Left = Left
.Width = Width
.Font.Size = 10
.Top = Top
End With
Set getNewControl = ctrl
End Function
Generally I'd set up classes and collections to hold references to your new controls.
It can work with your current set up though. First off I'll suggest an aesthetic change:
Set the size of your frame to a static size that fits on your screen and add the two command buttons outside of this.
Size the frame so it sits inside the bounds of your form.
Change the ScrollBars property to 2 - fmScrollBarsVertical.
In your code:
Add a new variable
Dim fme As Frame
Set fme = UserForm6.Frame1
Update your references to UserForm6 so they reference fme instead when you add the labels and combobox:
Set theLabel = fme.Add("Forms.label.1", "Test" & c, True)
.
.
Set ComboBox1 = fme.Controls.Add("Forms.combobox.1", "Test" & c, True)
.
.
Set theLabel = fme.Controls.Add("Forms.label.1", "Test" & c, True)
Outside your final loop add this line of code (you may have to play around with the maths to get the correct scroll height):
fme.ScrollHeight = buttonheight + 90
Remove the code that adds the two command buttons (as they're now static outside of the frame).
Now your whole form should sit on the page and you can scroll through the controls.
Double-click your command button to add a Click event to it:
Private Sub CommandButton1_Click()
Dim ctrl As Control
Dim x As Long
For Each ctrl In Me.Frame1.Controls
If TypeName(ctrl) = "ComboBox" Then
x = x + 1
ThisWorkbook.Worksheets("Sheet2").Cells(x, 1) = ctrl.Value
End If
Next ctrl
End Sub
The code will go through each combobox on the form and copy the selected value to Sheet2 in the workbook.
Edit:
All the code incorporating the changes I made.
Sub addLabel()
UserForm6.Show vbModeless
Dim theLabel As Object
Dim ComboBox1 As Object
Dim CommandApp As Object
Dim CommandCan As Object
Dim buttonheight As Long
Dim fme As Frame
Dim c As Variant
Dim labelCounter As Long
Set fme = UserForm6.Frame1
For Each c In Sheets("Sheet1").Range("A1:A100")
If c.Value = "" Then Exit For
Set theLabel = fme.Add("Forms.label.1", "Test" & c, True)
With theLabel
.Caption = c
.Left = 10
.Width = 50
.Height = 20
.Font.Size = 10
If c.Row = 1 Then
.Top = 34
Else
.Top = 25 + (20 * (c.Row - 1)) + 9
End If
End With
Set ComboBox1 = fme.Controls.Add("Forms.combobox.1", "Test" & c, True)
With ComboBox1
.AddItem "Approved"
.AddItem "Partially Approved"
.AddItem "Not Approved"
.Left = 190
.Width = 120
.Height = 20
.Font.Size = 10
If c.Row = 1 Then
.Top = 30
Else
.Top = 30 + (20 * (c.Row - 1))
buttonheight = 30 + (20 * (c.Row - 1))
End If
End With
Next c
For Each c In Sheets("Sheet1").Range("B1:B100")
If c.Value = "" Then Exit For
Set theLabel = fme.Controls.Add("Forms.label.1", "Test" & c, True)
With theLabel
.Caption = c
.Left = 90
.Width = 70
.Height = 20
.Font.Size = 10
If c.Row = 1 Then
.Top = 34
Else
.Top = 25 + (20 * (c.Row - 1)) + 9
End If
End With
Next c
fme.ScrollHeight = buttonheight + 90
End Sub

How to add text box and command button in excel user form during run time?

I want to add n number of text box and one command button during run time of the user form in excel - vba .
While the number 'n' , i am getting through a cell value .
I want to store the data entered ( in the dynamically during run time created text box when the user clicks on Submit button that is also run time created )
in excel sheet .
For i = 1 To ssheet.Cells(2, 2).Value
Set txtB1 = Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "d" & i
.Height = 25
.Width = 150
.Left = 105
.Top = 20 + 10 * i * 4
End With
Set cCont = Controls.Add("Forms.CommandButton.1", "Button", True)
With cCont
.Caption = "Submit"
.Top = 60 + 10 * ssheet.Cells(2, 2).Value * 4
.Left = 105
End With
Here i am able to display as required but unable to trigger the users button click and store values in excel sheet .
So create a userform and add the command button in design time like below
Add the following code in the user form
Private Sub CommandButton1_Click()
For i = 1 To ssheet.Cells(2, 2).Value
ssheet.Cells(i, 5).Value = Controls("d" & i).Value
Next i
Unload UserForm1
End Sub
Private Sub UserForm_Initialize()
If ssheet.Cells(2, 2).Value > 0 Then
For i = 1 To ssheet.Cells(2, 2).Value
Set txtB1 = Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "d" & i
.Height = 25
.Width = 150
.Left = 10
.Top = 30 * (i - 1) + 5
End With
Next i
With CommandButton1
.Caption = "Submit"
.Top = 30 * (i - 1) + 5
.Left = 10
End With
With Me
.Width = 200
.Height = 200
.ScrollTop = 0
.KeepScrollBarsVisible = fmScrollBarsVertical
.ScrollBars = fmScrollBarsVertical
.ScrollHeight = 30 * i + 5
End With
Else
CommandButton1.Visible = False
End If
End Sub
You can then call the userform from workbook module
Private Sub Workbook_Open()
UserForm1.Show
End Sub
When you load the form, the text boxes will be created and aligned based on B2 cell's value

create text box by row & column on a userform vba

Could any one direct me in right direction for the following code? I want to create a text box at run time by row & column.The following creates only a row & not multiple rows. I want the columns to remain same & just keep increasing rows. Thanks in advance :)
Dim txtB1 As Control
Dim i
For i = 0 To 4
Set txtB1 = UserForm.Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "chkDemo" & i
.Height = 20
.Width = 50
.Left = 30 * i * 2
.Top = 15
.ControlTipText = "Type of Bug"
End With
Next i
You need a For loop for each dimension (rows and columns).
Dim txtB1 As Control
Dim i, jrow
For jrow = 1 To 5
For i = 0 To 4
Set txtB1 = UserForm.Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "chkDemo" & i
.Height = 20
.Width = 50
.Left = 50 * i + 2
.Top = 20 * jrow + 15
.ControlTipText = "Type of Bug"
End With
Next i
Next jrow
Result:

Doesn't want to add controls on a specific page

I'm running into an unusual problem.
I have a set of procedures to add a bunch of controls on different pages
One to add the frames
Public Sub AddFramesNP(form, pagina, nrpag)
Set cControl = form!main.Pages(nrpag).Controls.Add("Forms.Frame.1", "io" & masina, True)
With cControl
.Caption = "IO"
.Width = 210
.Height = 360
.Top = 2
.Left = 5
End With
Set cControl = form!main.Pages(nrpag).Controls.Add("Forms.Frame.1", "nio" & masina, True)
With cControl
.Caption = "nIO"
.Width = 210
.Height = 360
.Top = 2
.Left = 220
End With
Set cControl = form!main.Pages(nrpag).Controls.Add("Forms.Frame.1", "desc" & masina, True)
With cControl
.Caption = "Descriere"
.Width = 210
.Height = 360
.Top = 2
.Left = 435
End With
End Sub
One to add the Labels
Public Sub AddLabsNP(form, pagina, replicare, den, den1, den2)
Dim k, l As Integer
If den = 1 Then
Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lden1" & pagina, True)
With cControl
.Caption = den1
.Width = 40
.Height = 10
.Top = 5
.Left = 5
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lden1nio" & pagina, True)
With cControl
.Caption = den1
.Width = 40
.Height = 10
.Top = 5
.Left = 5
End With
End If
If replicare = 1 Then
Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lden2" & pagina, True)
With cControl
.Caption = den2
.Width = 40
.Height = 10
.Top = 165
.Left = 5
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lden2nio" & pagina, True)
With cControl
.Caption = den2
.Width = 40
.Height = 10
.Top = 165
.Left = 5
End With
End If
Do While l < replicare + 1
Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lreper" & l & pagina, True)
With cControl
.Caption = "Reper"
.Width = 35
.Height = 9
.Top = 25 + k
.Left = 5
End With
Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lsn" & l & pagina, True)
With cControl
.Caption = "SN"
.Width = 35
.Height = 9
.Top = 25 + k
.Left = 70
End With
Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lqt" & l & pagina, True)
With cControl
.Caption = "Qt"
.Width = 35
.Height = 9
.Top = 25 + k
.Left = 155
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lrepernio" & l & pagina, True)
With cControl
.Caption = "Reper"
.Width = 35
.Height = 9
.Top = 25 + k
.Left = 5
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lsnnio" & l & pagina, True)
With cControl
.Caption = "SN"
.Width = 35
.Height = 9
.Top = 25 + k
.Left = 70
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lqtnio" & l & pagina, True)
With cControl
.Caption = "Qt"
.Width = 35
.Height = 9
.Top = 25 + k
.Left = 155
End With
k = k + 155
l = l + 1
Loop
End Sub
And one to add the ComboBoxes
Public Sub AddCboxsNP(form, pagina, replicare, nrcboxs)
Dim k, l As Integer
l = 1
Do While l < nrcboxs + 1
Set cControl = form.Controls("io" & masina).Add("Forms.ComboBox.1", "combo" & l & pagina, True)
With cControl
.Width = 60
.Height = 14
.Top = 40 + k
.Left = 5
End With
Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "sn" & l & pagina, True)
With cControl
.Width = 80
.Height = 28
.Top = 40 + k
.Left = 70
End With
Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "q" & l & pagina, True)
With cControl
.Width = 30
.Height = 14
.Top = 40 + k
.Left = 155
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.ComboBox.1", "combo" & l & "nio" & pagina, True)
With cControl
.Width = 60
.Height = 14
.Top = 40 + k
.Left = 5
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "sn" & l & "nio" & pagina, True)
With cControl
.Width = 80
.Height = 28
.Top = 40 + k
.Left = 70
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "q" & l & "nio" & pagina, True)
With cControl
.Width = 30
.Height = 14
.Top = 40 + k
.Left = 155
End With
If replicare = 2 Then
Set cControl = form.Controls("io" & masina).Add("Forms.ComboBox.1", "combo" & l & "2" & pagina, True)
With cControl
.Width = 60
.Height = 14
.Top = 200 + k
.Left = 5
End With
Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "sn" & l & "2" & pagina, True)
With cControl
.Width = 80
.Height = 28
.Top = 200 + k
.Left = 70
End With
Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "q" & l & "2" & pagina, True)
With cControl
.Width = 30
.Height = 14
.Top = 200 + k
.Left = 155
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.ComboBox.1", "combo" & l & "2nio" & pagina, True)
With cControl
.Width = 60
.Height = 14
.Top = 200 + k
.Left = 5
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "sn" & l & "2nio" & pagina, True)
With cControl
.Width = 80
.Height = 28
.Top = 200 + k
.Left = 70
End With
Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "q" & l & "2nio" & pagina, True)
With cControl
.Width = 30
.Height = 14
.Top = 200 + k
.Left = 155
End With
End If
k = k + 35
l = l + 1
Loop
End Sub
The problem is that when I want to use them I don't know why but they don't work for page 3 (I use them for page 2 and they work fine). I have to use this ones just for these 2 pages (2 and 3). And I have another set of procedures for the pages that contain multipages that work fine for 3 pages.
I really don't see where's the problem. I tried to add components manually (through code) and it worked fine. Did I do something that makes this procedures to work just 1 time? I don't see what because are the same with other procedures that work multiple times!
Thanks a lot for your help!
I know it is an old thread but just the same... :)
I am taking the Frames Code as an example. Please use it as a sample for the rest :)
Also this example creates the frames in a MultiPage1.
Let me know if this helps :)
Option Explicit
Private Sub CommandButton1_Click()
AddFramesNP Me, 3, 2
End Sub
Public Sub AddFramesNP(form As UserForm, masina, nrpag)
Dim cControl As Object
With form
Set cControl = .MultiPage1.Pages(nrpag).Controls.Add("Forms.Frame.1", "io" & masina)
With cControl
.Caption = "IO"
.Width = 210: .Height = 360: .Top = 2
.Left = 5
End With
Set cControl = .MultiPage1.Pages(nrpag).Controls.Add("Forms.Frame.1", "nio" & masina)
With cControl
.Caption = "nIO"
.Width = 210: .Height = 360: .Top = 2
.Left = 220
End With
Set cControl = .MultiPage1.Pages(nrpag).Controls.Add("Forms.Frame.1", "desc" & masina)
With cControl
.Caption = "Descriere"
.Width = 210: .Height = 360: .Top = 2
.Left = 435
End With
End With
End Sub