VBA TextBox Array with Events - vba

I'm trying to create a dynamic array of textboxes to handle any amount of data that the user decides to introduce. The problem is, that now I need those textboxes to have a _Change event handler. I have read that you can define controls with events this way:
Public WithEvents txt as MSForms.textbox
But it doesn't let me define it as an array. Does anyone knows how to do this? Here's my code:
Public numacc As Integer
Dim NombreAcc() As MSForms.textbox
Dim k() As MSForms.textbox
Dim Cant() As MSForms.textbox
Private Sub CommandButton1_Click()
If numacc = 0 Then
DatosTuberia.Height = DatosTuberia.Height + 18
Errores.Top = Errores.Top + 18
LblNombre.Visible = True
LblK.Visible = True
LblCantidad.Visible = True
End If
DatosTuberia.Height = DatosTuberia.Height + 18
Errores.Top = Errores.Top + 18
ReDim Preserve NombreAcc(numacc), k(numacc), Cant(numacc)
Set NombreAcc(numacc) = DatosTuberia.Controls.Add("Forms.TextBox.1", "Nombre")
Set k(numacc) = DatosTuberia.Controls.Add("Forms.TextBox.1", "k")
Set Cant(numacc) = DatosTuberia.Controls.Add("Forms.TextBox.1", "Cantidad")
With NombreAcc(numacc)
.Top = 262 + numacc * 18
.Left = 14
.Width = 76
.Height = 18
.BorderStyle = fmBorderStyleSingle
.Font.Size = 12
End With
With k(numacc)
.Top = 262 + numacc * 18
.Left = 89.5
.Width = 76
.Height = 18
.BorderStyle = fmBorderStyleSingle
.Font.Size = 12
End With
With Cant(numacc)
.Top = 262 + numacc * 18
.Left = 164
.Width = 76
.Height = 18
.BorderStyle = fmBorderStyleSingle
.Font.Size = 12
End With
numacc = numacc + 1
End Sub

Related

How to determine the .Left value of a control?

I'm building my first user interface in VBA on Microsoft Access.
I am trying to get the .Left variable to show up in the drop down selection (library?).
The only thing that pops up is LeftPadding, which I'm pretty sure that isn't what I need. Why am I not able to declare the Left position of the rectangles?
Is there another type of variable that I should be using to declare the position of rectangles?
My follow up issue, if I'm doing that correctly, is about a nested If statement. I'm trying to calculate whether a newly visible rectangle's position + its dimensions exceeds the Left position of an already visible rectangle, and if so, position it elsewhere.
Dim ctl As Control
For Each ctl In [Forms]![frmBuilder]
If Left(ctl.Name, 3) = "box" And Box1.Visible = True Then
If ctl.Visible = True Then
NextCaseNum = Int(Right(ctl.Name, (Len(ctl.Name)) - 3) + 1)
NextCasePosition = (ctl.lef + ctl.Width) + 1440 / 60
NextCaseName = "box" & NextCaseNum
Else
CurCaseLeft = ctl.Left
CurCaseWidth = ctl.Width
CurCaseHeight = ctl.Height
With ctl
.Top = UprightBottom - HInch
.Left = NextCasePosition
.Width = WInch
.Height = HInch
.Visible = True
End With
If CurCaseLeft + CurCaseWidth > Upright2.Left Then
With Beam1
.Top = (((5.5 + 6) * 60) + Box1.Top) / 1440
.Left = Upright1.Left
.Height = (5.5 * 60) / 1440
.Width = ((4 * 60) / 1440) + Upright2.Left - Upright1.Left
.Visible = True
End With
End If
I think the problem lies with CurCaseLeft and CurCaseWidth, because I don't know how to define them in the function due to the current box's ctl.Left not showing up.
Do I have to separate the nested If statement in to a different function and call that function from the current function?
Try to be more explicit:
Dim ctl As Control
Dim rct As Rectangle
For Each ctl In [Forms]![frmBuilder]
If Left(ctl.Name, 3) = "box" And Box1.Visible = True Then
If ctl.Visible = True Then
Set rct = ctl
NextCaseNum = Int(Right(rct.Name, (Len(rct.Name)) - 3) + 1)
NextCasePosition = (rct.Left + rct.Width) + 1440 / 60

VBA UserForm Space Between Labels Algorithm

I have an algorithm in code of userform that I'm using to add names of months to a VBA userform dynamically. I want to add the months 3 times as an header for 3 different categories. My problem is that in the Algorithm I created the space between the two first months is one and between the second month and the third the space is double. the months values is in worksheet 8 in the cells A4 to A7.
it looks like this in the uFebruary
december ___January ______February
Here is my code:
'months in commission, workdays and workhours
Dim m As Integer 'm = month
Dim T As Integer 'T=TOP
Dim L As Integer 'L= loop
T = 50
For m = 1 To 3
For L = 1 To 9
DATA = ThisWorkbook.Worksheets(8).Range("A" & m + 3).Value
Set dLbl = UserForm1.Controls.Add("Forms.Label.1", "dLbl", True)
With dLbl
.Top = 70
If L < 4 Then
.Left = 700 - (T * m)
ElseIf L > 6 Then
.Left = 190 - (T * m)
Else
.Left = 450 - (T * m)
End If
.Height = 50
.Caption = DATA
.Font.Bold = True
.Font.Size = 11
.Width = 45
.TextAlign = fmTextAlignRight
End With
Next L
Next m

fit controls to screen vba

i'm trying to fit the user form to screen on diifernet screens.
the userform was first managed in my work screen and i fit it to my screen but when i'm trying the userform on other screens part of it vanished.
i can't put the whole code in here but i will put just the sub that suppose to fit to screen:
Private Sub UserForm_Initialize()
Dim w As Long, h As Long
Application.Visible = False
With Me
rMaxHeight = Application.Height
rMaxWidth = Application.Width
If .Height > Application.Height - 10 Then
rNormalHeight = rMaxHeight * 0.85
Else
rNormalHeight = Me.Height
End If
If .Width > Application.Width - 10 Then
rNormalWidth = rMaxWidth * 0.85
Else
rNormalWidth = Me.Width
End If
.StartUpPosition = 1
.Left = 0
.Top = 0
FitSize
...
Private Sub FitSize()
Dim h, w
Dim c As Control
Dim PHeight, PWidth As Double
PHeight = rNormalHeight / Me.Height
PWidth = rNormalWidth / Me.Width
h = 0: w = 0
If PHeight = 1 And PWidth = 1 Then Exit Sub
For Each c In Me.Controls
If c.Visible Then
If c.Top + c.Height > h Then h = (c.Top + c.Height) ' * PHeight
If c.Left + c.Width > w Then w = (c.Left + c.Width) ' * PWidth
If Not TypeName(c) = "Image" Or TypeName(c) = "ListBox" Then c.FontSize = c.FontSize * ((PHeight + PWidth) / 2)
End If
Next c
If h > 0 And w > 0 Then
With Me
.Width = w + 40
.Height = h + 40
End With
End If
End Sub
hope you could help me with that
Thank you all
sefi
You can either Re-position every single control in the UserForm with VBA or simply enable ScrollBars for the UserForm object so they can access all the elements with a bit of scrolling.
Change the ScrollBars property of the UserForm to like 3 - fmScrollBarsBoth as the default is 0 - fmScrollBarsNone
Then you need to figure out how tall and wide it needs to be:
ScrollHeight
ScrollWidth
Hello and thank for everyone that tried to help me.
I found the solution to this problem by fitting the controls to the proportion of the screen copared with the original form.
At first step you need to calculate the proportion:
Dim PHeight, PWidth As Double
'define form size compared with the original size of the form
rMaxHeight = Application.Height
rMaxWidth = Application.Width
If Me.Height > Application.Height Then
rNormalHeight = rMaxHeight * 0.85
Else
rNormalHeight = Me.Height
End If
If Me.Width > Application.Width Then
rNormalWidth = rMaxWidth * 0.85
Else
rNormalWidth = Me.Width
End If
'normal is the size needed in normal mode before the form get to maximize mode
'we want to calculate the needed divided to the orignal
PHeight = rNormalHeight / Me.Height
PWidth = rNormalWidth / Me.Width
now we call fitsize()
Private Sub FitSize()
Dim h, w
Dim c As Control
h = 0: w = 0
If PHeight = 1 And PWidth = 1 Then Exit Sub ' if the it is the original size of the form- don't bother...
'loop on the form controls
For Each c In Me.Controls
If c.Visible Then ' just visible controls
c.Top = c.Top * PHeight ' fit to proportion of the screen compared with the original form
c.Height = c.Height * PHeight
If c.Top + c.Height > h Then h = c.Top + c.Height ' collect the height needed from the controls
c.Left = c.Left * PWidth ' fit to proportion of the screen compared with the original form
c.Width = c.Width * PWidth
If c.Left + c.Width > w Then w = c.Left + c.Width ' collect the height needed from the controls
'fit the font for the text controls
If Not TypeName(c) = "Image" Or TypeName(c) = "ListBox" Then c.FontSize = c.FontSize * ((PHeight + PWidth) / 2)
End If
Next c
'define the size needed form the specific screen
If h > 0 And w > 0 Then
With Me
.Width = w + 40
.Height = h + 40
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
End With
End If
End Sub
this code will define the size needed in each screen by the proportion that calculated in the needed value divded to the original value.
Try it and tell me if it works.
thank you all
sefi

Adding event listener to several comboboxes

I'm basically trying to display different sets of textboxes on a userform depending on the value taken by a combobox. I have created a class module called CControlEvents in which I describe the events that should occur when I change the value of the combobox :
Private WithEvents mclsCbx As MSForms.ComboBox
Private WithEvents UnitmclsTbx As MSForms.TextBox
Private WithEvents UnitmclsTbxLabel As MSForms.Label
Private WithEvents SpecMinMoymclsTbxLabel As MSForms.Label
Private WithEvents SpecMaxMoymclsTbxLabel As MSForms.Label
Private WithEvents SpecMinIndmclsTbxLabel As MSForms.Label
Private WithEvents SpecMaxIndmclsTbxLabel As MSForms.Label
Private WithEvents SpecMinMoymclsTbx As MSForms.TextBox
Private WithEvents SpecMaxMoymclsTbx As MSForms.TextBox
Private WithEvents SpecMinIndmclsTbx As MSForms.TextBox
Private WithEvents SpecMaxIndmclsTbx As MSForms.TextBox
Private WithEvents ListmclsTbxLabel As MSForms.Label
Private WithEvents ListmclsTbx As MSForms.TextBox
Private mMyProperty As Integer
Public Property Set Cbx(ByVal clsCbx As MSForms.ComboBox): Set mclsCbx = clsCbx: End Property
Public Property Get Cbx() As MSForms.ComboBox: Set Cbx = mclsCbx: End Property
'Property pour les textbox et labels
'TextBox d'unité
Public Property Set UnitTbx(ByVal clsTbx As MSForms.TextBox): Set UnitmclsTbx = clsTbx: End Property
Public Property Get UnitTbx() As MSForms.TextBox: Set UnitTbx = UnitmclsTbx: End Property
'Label d'unité
Public Property Set UnitTbxLabel(ByVal clsTbx As MSForms.Label): Set UnitmclsTbxLabel = clsTbx: End Property
Public Property Get UnitTbxLabel() As MSForms.Label: Set UnitTbxLabel = UnitmclsTbxLabel: End Property
'TextBox de spécification minimum moyenne
Public Property Set SpecMinMoyTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMinMoymclsTbx = clsTbx: End Property
Public Property Get SpecMinMoyTbx() As MSForms.TextBox: Set SpecMinMoyTbx = SpecMinMoymclsTbx: End Property
'Label de spécification minimum moyenne
Public Property Set SpecMinMoyTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMinMoymclsTbxLabel = clsTbx: End Property
Public Property Get SpecMinMoyTbxLabel() As MSForms.Label: Set SpecMinMoyTbxLabel = SpecMinMoymclsTbxLabel: End Property
'Label de spécification maximum moyenne
Public Property Set SpecMaxMoyTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMaxMoymclsTbxLabel = clsTbx: End Property
Public Property Get SpecMaxMoyTbxLabel() As MSForms.Label: Set SpecMaxMoyTbxLabel = SpecMaxMoymclsTbxLabel: End Property
'TextBox de spécification max moy
Public Property Set SpecMaxMoyTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMaxMoymclsTbx = clsTbx: End Property
Public Property Get SpecMaxMoyTbx() As MSForms.TextBox: Set SpecMaxMoyTbx = SpecMaxMoymclsTbx: End Property
'TextBox de spécification minimum individuelle
Public Property Set SpecMinIndTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMinIndmclsTbx = clsTbx: End Property
Public Property Get SpecMinIndTbx() As MSForms.TextBox: Set SpecMinIndTbx = SpecMinIndmclsTbx: End Property
'Label de spécification minimum individuelle
Public Property Set SpecMinIndTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMinIndmclsTbxLabel = clsTbx: End Property
Public Property Get SpecMinIndTbxLabel() As MSForms.Label: Set SpecMinIndTbxLabel = SpecMinIndmclsTbxLabel: End Property
'Label de spécification maximum individuelle
Public Property Set SpecMaxIndTbxLabel(ByVal clsTbx As MSForms.Label): Set SpecMaxIndmclsTbxLabel = clsTbx: End Property
Public Property Get SpecMaxIndTbxLabel() As MSForms.Label: Set SpecMaxIndTbxLabel = SpecMaxIndmclsTbxLabel: End Property
'TextBox de spécification max individuelle
Public Property Set SpecMaxIndTbx(ByVal clsTbx As MSForms.TextBox): Set SpecMaxIndmclsTbx = clsTbx: End Property
Public Property Get SpecMaxIndTbx() As MSForms.TextBox: Set SpecMaxIndTbx = SpecMaxIndmclsTbx: End Property
'TextBox de liste déroulante
Public Property Set ListTbx(ByVal clsTbx As MSForms.TextBox): Set ListmclsTbx = clsTbx: End Property
Public Property Get ListTbx() As MSForms.TextBox: Set ListTbx = ListmclsTbx: End Property
'Label de liste déroulante
Public Property Set ListTbxLabel(ByVal clsTbx As MSForms.Label): Set ListmclsTbxLabel = clsTbx: End Property
Public Property Get ListTbxLabel() As MSForms.Label: Set ListTbxLabel = ListmclsTbxLabel: End Property
Public Property Get MyProperty() As Integer
MyProperty = mMyProperty
End Property
Public Property Let Transition(Value As Integer)
mMyProperty = Value
End Property
Private Sub mclsCbx_Change()
If (Me.Cbx.Text = "NUM") Then
UnitTbx.Visible = True
UnitmclsTbxLabel.Visible = True
SpecMinMoymclsTbxLabel.Visible = True
SpecMinMoymclsTbx.Visible = True
SpecMaxMoymclsTbx.Visible = True
SpecMaxMoymclsTbxLabel.Visible = True
SpecMinIndmclsTbxLabel.Visible = True
SpecMinIndmclsTbx.Visible = True
SpecMaxIndmclsTbx.Visible = True
SpecMaxIndmclsTbxLabel.Visible = True
ListmclsTbx.Visible = False
ListmclsTbxLabel.Visible = False
Else
If (Me.Cbx.Text = "LIST") Then
UnitTbx.Visible = False
UnitmclsTbxLabel.Visible = False
SpecMinMoymclsTbxLabel.Visible = False
SpecMinMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbxLabel.Visible = False
SpecMinIndmclsTbxLabel.Visible = False
SpecMinIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbxLabel.Visible = False
ListmclsTbx.Visible = True
ListmclsTbxLabel.Visible = True
End If
End If
End Sub
In the userform's code, I dynamically add such comboboxes :
Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set gclsControlEvents.Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set gclsControlEvents.UnitTbx = numUnitTextBox
'Label unité
Set gclsControlEvents.UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set gclsControlEvents.SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxMoyTbx = SpecMaxMoymclsTbx
So, I basically add the controls in the userform code and assign them to properties of the class, then I can simply hide/show as appropriate. The problem is that when I try to add several comboboxes based on this principle, using a for loop, it works only for the last comboboxe.
EDIT : The for loop
For i = 1 To NewSheetModelColumnsCount
Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
Set txtB1 = Frame1.Add("Forms.TextBox.1")
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
With txtB1
.name = "chkDemo" & i
.Height = 15
.Width = 100
.Left = 230
.Top = 10 * i * 3
End With
Lbl.Caption = "Colonne n°" + CStr(i)
Lbl.Top = txtB1.Top
Lbl.Left = 10
Set lbl2 = Frame1.Controls.Add("Forms.Label.1", "lbl2")
lbl2.Caption = "Intitulé : "
lbl2.Top = txtB1.Top
lbl2.Left = 180
lbl2.Visible = True
lbl2.Width = 50
Set UnitmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "UnitmclsTbxLabel")
Set ListmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "ListmclsTbxLabel")
Set SpecMinMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMaxMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMinIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
Set SpecMaxIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
UnitmclsTbxLabel.Caption = "Unité : "
UnitmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Visible = False
Set numUnitTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set ListTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
SpecMinMoymclsTbx.Width = 50
SpecMaxMoymclsTbx.Width = 50
SpecMinIndmclsTbx.Width = 50
SpecMaxIndmclsTbx.Width = 50
With numUnitTextBox
.name = "Unit" & i
.Height = 15
.Width = 50
.Left = 360 + UnitmclsTbxLabel.Width - 40
.Top = 10 * i * 3
.Value = "Unit"
End With
ListTextBox.Top = numUnitTextBox.Top
ListTextBox.Left = numUnitTextBox.Left + numUnitTextBox.Width - 40
ListTextBox.Visible = False
ListTextBox.Width = 200
ListmclsTbxLabel.Caption = "Options : "
ListmclsTbxLabel.Visible = False
ListmclsTbxLabel.Top = numUnitTextBox.Top
ListmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Left = numUnitTextBox.Left + numUnitTextBox.Width + 5
SpecMinMoyTbxLabel.Caption = "Spéc min Moy : "
SpecMinMoyTbxLabel.Visible = False
SpecMinMoymclsTbx.Top = numUnitTextBox.Top
SpecMinMoymclsTbx.Visible = False
SpecMinMoymclsTbx.Left = SpecMinMoyTbxLabel.Left + SpecMinMoyTbxLabel.Width - 15
SpecMaxMoyTbxLabel.Visible = False
SpecMaxMoyTbxLabel.Caption = "Spéc max moy : "
SpecMaxMoyTbxLabel.Left = SpecMinMoymclsTbx.Left + SpecMinMoymclsTbx.Width
SpecMaxMoyTbxLabel.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Left = SpecMaxMoyTbxLabel.Left + SpecMaxMoyTbxLabel.Width
SpecMinIndTbxLabel.Top = numUnitTextBox.Top
SpecMinIndTbxLabel.Left = SpecMaxMoymclsTbx.Left + SpecMaxMoymclsTbx.Width + 5
SpecMinIndTbxLabel.Caption = "Spéc min Ind : "
SpecMinIndTbxLabel.Visible = False
SpecMinIndmclsTbx.Top = numUnitTextBox.Top
SpecMinIndmclsTbx.Visible = False
SpecMinIndmclsTbx.Left = SpecMinIndTbxLabel.Left + SpecMinIndTbxLabel.Width - 15
SpecMaxIndTbxLabel.Visible = False
SpecMaxIndTbxLabel.Caption = "Spéc max moy : "
SpecMaxIndTbxLabel.Left = SpecMinIndmclsTbx.Left + SpecMinIndmclsTbx.Width
SpecMaxIndTbxLabel.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Left = SpecMaxIndTbxLabel.Left + SpecMaxIndTbxLabel.Width
numUnitTextBox.Visible = False
Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set gclsControlEvents.Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set gclsControlEvents.UnitTbx = numUnitTextBox
'Label unité
Set gclsControlEvents.UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set gclsControlEvents.SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxMoyTbx = SpecMaxMoymclsTbx
'Label spéc moy min
Set gclsControlEvents.SpecMinIndTbxLabel = SpecMinIndTbxLabel
'Zone de texte spéc moy min
Set gclsControlEvents.SpecMinIndTbx = SpecMinIndmclsTbx
'Label spéc max min
Set gclsControlEvents.SpecMaxIndTbxLabel = SpecMaxIndTbxLabel
'Zone de texte spéc moy max
Set gclsControlEvents.SpecMaxIndTbx = SpecMaxIndmclsTbx
'Zone de texte des options de la liste déroulante
Set gclsControlEvents.ListTbx = ListTextBox
'Label Options liste déroulante
Set gclsControlEvents.ListTbxLabel = ListmclsTbxLabel
Let gclsControlEvents.Transition = i
Next i
EDIT :
I tried to solve the problem by creating a variant array of as many instances of the class CControlEvents as I need comboboxes then using the element of this array at index i-1 at each iteration, but it didn't work, here is the code :
If (NewSheetModelColumnsCount > 0) Then
For i = 1 To 6
Controls("headerTextBox" + CStr(i)).Visible = True
Controls("CheckBox" + CStr(i)).Visible = True
Next i
For i = 11 To 16
Controls("label" + CStr(i)).Visible = True
Next i
Me.indicationLabel.Visible = False
'Name of the frame
With Me.Frame1
'This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'Change the values of 2 as Per your requirements
.ScrollHeight = 30 * NewSheetModelColumnsCount + 50
.ScrollWidth = 30 * 50 + 50
'
End With
'Tentative
Dim classList As Variant
ReDim classList(0 To NewSheetModelColumnsCount - 1)
For i = 0 To NewSheetModelColumnsCount - 1
Set classList(i) = New CControlEvents
Next i
For i = 1 To NewSheetModelColumnsCount
Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
Set txtB1 = Frame1.Add("Forms.TextBox.1")
' Set ListBoxB1 = Frame1.Controls.Add("Forms.ComboBox.1")
' With ListBoxB1
' .Name = "list" & i
' .Height = 15
' .Width = 100
' .Left = 70
' .Top = 10 * i * 3
' .AddItem "NUM"
' .AddItem "LIST"
' End With
'Create the combobox
Set oleCbx = Frame1.Add("Forms.ComboBox.1") 'Bug at this line
With oleCbx
.name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
With txtB1
.name = "chkDemo" & i
.Height = 15
.Width = 100
.Left = 230
.Top = 10 * i * 3
End With
Lbl.Caption = "Colonne n°" + CStr(i)
Lbl.Top = txtB1.Top
Lbl.Left = 10
Set lbl2 = Frame1.Controls.Add("Forms.Label.1", "lbl2")
'Set txtB2 = Frame1.Add("Forms.TextBox.1")
' With txtB2
' .name = "unitTextBox" & i
' .Height = 15
' .Width = 100
' .Left = 240
' .Top = 10 * i * 3
' .Value = "txtB2"
' End With
lbl2.Caption = "Intitulé : "
lbl2.Top = txtB1.Top
lbl2.Left = 180
lbl2.Visible = True
lbl2.Width = 50
Set UnitmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "UnitmclsTbxLabel")
Set ListmclsTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "ListmclsTbxLabel")
Set SpecMinMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMaxMoyTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinMoyTbxLabel")
Set SpecMinIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
Set SpecMaxIndTbxLabel = UserForm1.Frame1.Controls.Add("Forms.Label.1", "SpecMinIndTbxLabel")
UnitmclsTbxLabel.Caption = "Unité : "
UnitmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Visible = False
Set numUnitTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxMoymclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMinIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set SpecMaxIndmclsTbx = UserForm1.Frame1.Add("Forms.TextBox.1")
Set ListTextBox = UserForm1.Frame1.Add("Forms.TextBox.1")
SpecMinMoymclsTbx.Width = 50
SpecMaxMoymclsTbx.Width = 50
SpecMinIndmclsTbx.Width = 50
SpecMaxIndmclsTbx.Width = 50
With numUnitTextBox
.name = "Unit" & i
.Height = 15
.Width = 50
.Left = 360 + UnitmclsTbxLabel.Width - 40
.Top = 10 * i * 3
.Value = "Unit"
End With
ListTextBox.Top = numUnitTextBox.Top
ListTextBox.Left = numUnitTextBox.Left + numUnitTextBox.Width - 40
ListTextBox.Visible = False
ListTextBox.Width = 200
ListmclsTbxLabel.Caption = "Options : "
ListmclsTbxLabel.Visible = False
ListmclsTbxLabel.Top = numUnitTextBox.Top
ListmclsTbxLabel.Left = 360
UnitmclsTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Top = numUnitTextBox.Top
SpecMinMoyTbxLabel.Left = numUnitTextBox.Left + numUnitTextBox.Width + 5
SpecMinMoyTbxLabel.Caption = "Spéc min Moy : "
SpecMinMoyTbxLabel.Visible = False
SpecMinMoymclsTbx.Top = numUnitTextBox.Top
SpecMinMoymclsTbx.Visible = False
SpecMinMoymclsTbx.Left = SpecMinMoyTbxLabel.Left + SpecMinMoyTbxLabel.Width - 15
SpecMaxMoyTbxLabel.Visible = False
SpecMaxMoyTbxLabel.Caption = "Spéc max moy : "
SpecMaxMoyTbxLabel.Left = SpecMinMoymclsTbx.Left + SpecMinMoymclsTbx.Width
SpecMaxMoyTbxLabel.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Visible = False
SpecMaxMoymclsTbx.Top = numUnitTextBox.Top
SpecMaxMoymclsTbx.Left = SpecMaxMoyTbxLabel.Left + SpecMaxMoyTbxLabel.Width
SpecMinIndTbxLabel.Top = numUnitTextBox.Top
SpecMinIndTbxLabel.Left = SpecMaxMoymclsTbx.Left + SpecMaxMoymclsTbx.Width + 5
SpecMinIndTbxLabel.Caption = "Spéc min Ind : "
SpecMinIndTbxLabel.Visible = False
SpecMinIndmclsTbx.Top = numUnitTextBox.Top
SpecMinIndmclsTbx.Visible = False
SpecMinIndmclsTbx.Left = SpecMinIndTbxLabel.Left + SpecMinIndTbxLabel.Width - 15
SpecMaxIndTbxLabel.Visible = False
SpecMaxIndTbxLabel.Caption = "Spéc max moy : "
SpecMaxIndTbxLabel.Left = SpecMinIndmclsTbx.Left + SpecMinIndmclsTbx.Width
SpecMaxIndTbxLabel.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Visible = False
SpecMaxIndmclsTbx.Top = numUnitTextBox.Top
SpecMaxIndmclsTbx.Left = SpecMaxIndTbxLabel.Left + SpecMaxIndTbxLabel.Width
numUnitTextBox.Visible = False
'Set gclsControlEvents = New CControlEvents
'On attribue la comboBox à une propriété de la classe créée
Set classList(i - 1).Cbx = oleCbx
'On attribue une zone de texte à une propriété de la classe créée
Set classList(i - 1).UnitTbx = numUnitTextBox
'Label unité
Set classList(i - 1).UnitTbxLabel = UnitmclsTbxLabel
'Label spéc moy min
Set classList(i - 1).SpecMinMoyTbxLabel = SpecMinMoyTbxLabel
'Zone de texte spéc moy min
Set classList(i - 1).SpecMinMoyTbx = SpecMinMoymclsTbx
'Label spéc max min
Set classList(i - 1).SpecMaxMoyTbxLabel = SpecMaxMoyTbxLabel
'Zone de texte spéc moy max
Set classList(i - 1).SpecMaxMoyTbx = SpecMaxMoymclsTbx
'Label spéc moy min
Set classList(i - 1).SpecMinIndTbxLabel = SpecMinIndTbxLabel
'Zone de texte spéc moy min
Set classList(i - 1).SpecMinIndTbx = SpecMinIndmclsTbx
'Label spéc max min
Set classList(i - 1).SpecMaxIndTbxLabel = SpecMaxIndTbxLabel
'Zone de texte spéc moy max
Set classList(i - 1).SpecMaxIndTbx = SpecMaxIndmclsTbx
'Zone de texte des options de la liste déroulante
Set classList(i - 1).ListTbx = ListTextBox
'Label Options liste déroulante
Set classList(i - 1).ListTbxLabel = ListmclsTbxLabel
Let classList(i - 1).Transition = i
' Set Lbl3 = Frame1.Controls.Add("Forms.Label.1", "lbl3")
' Set txtB3 = Frame1.Add("Forms.TextBox.1")
' With txtB3
' .Name = "specMin" & i
' .Height = 15
' .Width = 200
' .Left = 410
' .Top = 10 * i * 3
' End With
' Lbl3.Caption = "Eléments : "
' Lbl3.Top = txtB3.Top
' Lbl3.Left = 360
Next i
'
'On modifie la visibilité des éléments pour ne permettre que la saisie du modèle courant
Me.RedefineModelParamButton.Visible = True
'
Me.Label1.Visible = False
Me.Label2.Visible = False
Me.Label3.Visible = False
Me.Label21.Visible = False
Me.NewSheetModelLabelTextBox.Visible = False
Me.NewSheetModelColumnsNumberTextBox.Visible = False
Me.SheetCategoryComboBox.Visible = False
Me.EnterNewSheetModelColumnsCountValidateButton.Visible = False
Me.EnterNewSheetModelColumnsCountCancelButton.Visible = False
Me.fcmIndexComboBox1.Visible = False
Me.fcmIndexComboBox2.Visible = False
End If
In the User-Form declare a VBA-Collection and store the instances of your class which wraps Combo-Boxes in this collection. In your class declare a Combo-Box variable WithEvents so you can handle events of the Combo-Box. HTH.
User Form
Private m_combos As New Collection
Private Sub UserForm_Initialize()
Set m_combos = New Collection
Dim i
Dim newCombo As CControlEvents
For i = 0 To 2
Set newCombo = New CControlEvents
With newCombo
Set .Combo = Me.Frame1.Add("Forms.ComboBox.1")
.Combo.Top = 20 * i
.Combo.AddItem "A"
.Combo.AddItem "B"
.Combo.AddItem "C"
End With
m_combos.Add newCombo
Next
End Sub
CControlEvents
Private WithEvents m_combo As MSForms.ComboBox
Private Sub m_combo_Change()
MsgBox "Change: " & Me.Combo.Name
End Sub
Public Property Get Combo() As MSForms.ComboBox
Set Combo = m_combo
End Property
Public Property Set Combo(ByVal newCombo As MSForms.ComboBox)
Set m_combo = newCombo
End Property

Declare & format multiple labels for a form using for loop

NET developers.
I'm trying to put 20 labels on a form and place them line by line (I do this by the .Top method). I am sure there is a way I can program declaring and formatting by looping through more general code 20 times.
The below is what I've done for the first label.
Thanks in advance for help!
Dim Label1 As New Label
Me.Controls.Add(Label1)
For m = 1 To OutlookManager3.GlobalVariables.SelectedAppointmentsNo
With Label1
.Width = 512
.Height = 18
.Top = subject.Top + subject.Height + m * 6 + (m - 1) * 18 + (m - 1) * 6
.Left = 12
.Text = "label" & m
End With
Next
You should place this as the first line inside your loop:
Dim Label1 As New Label
And this as the last line insde your loop:
Me.Controls.Add(Label1)
Example 1
For m = 1 To OutlookManager3.GlobalVariables.SelectedAppointmentsNo
Dim Label1 As New Label
With Label1
.Width = 512
.Height = 18
.Top = subject.Top + subject.Height + m * 6 + (m - 1) * 18 + (m - 1) * 6
.Left = 12
.Text = "label" & m
End With
Me.Controls.Add(Label1)
Next
Example 2
For m = 1 To OutlookManager3.GlobalVariables.SelectedAppointmentsNo
Me.Controls.Add(New Label() With {.Width = 512, .Height = 18, .Top = (subject.Top + subject.Height + m * 6 + (m - 1) * 18 + (m - 1) * 6), .Left = 12, .Text = ("label" & m)})
Next
you can use your code by place declare statement inside loop
For m = 1 To OutlookManager3.GlobalVariables.SelectedAppointmentsNo
Dim Label1 As New Label
With Label1
.Width = 512
.Height = 18
.Top = subject.Top + subject.Height + m * 6 + (m - 1) * 18 + (m - 1) * 6
.Left = 12
.Text = "label" & m
End With
Me.Controls.Add(Label1)
Next
or use panel just like this but you have to place declare statement inside loop
or should make label array for future reference by
Dim label(yoursize) As Label
For m = 1 To OutlookManager3.GlobalVariables.SelectedAppointmentsNo
label(m) = new label
label(m).ID="future referece id"
With Label1
.Width = 512
.Height = 18
.Top = subject.Top + subject.Height + m * 6 + (m - 1) * 18 + (m - 1) * 6
.Left = 12
.Text = "label" & m
End With
by this you can use that next time
Me.Controls.Add(Label1)
Next