Help With adding multiple controls during runtime based on button click

aholts

Board Regular
Joined
Oct 15, 2011
Messages
75
Hello,

I would like to see if it is possible to control the addition of Labels and text boxes based on a button click to my multipage object on my form. My initial thought was to use a case statement that would count the button clicks and add the labels and text boxes as needed. However, if I loop it of course I get all the labels and texts boxes at one time if I don't loop it I only get the one case for which the label and text box condition is true (resulting in only one Label and Textbox being created). Below is the code I am working with. Please let me know if there is a possible solution that I can add my these objects during runtime based on a button click.

The following is the image of my form and below that is the code. I feel like it could be as simple as properly declaring my variables correctly. I have thought of some work arounds but I would like to attempt to make this as concise as possible.

Thanks
Aholts

Code:
Private Sub CmdBtn_AddNewGroup_Click()
Dim i As Integer
Dim BClicks As Integer

BClicks  = 1
i = 1
For BClicks  = 1 To 10


Select Case BClicks


Case 1
' Draw the labels
Set NewUnitGrpLbl = MultiPage1.Pages(1).Controls.Add("Forms.label.1")


With NewUnitGrpLbl
    .Top = 118
    .Left = 18
    .Height = 18
    .Name = "LBL_UnitGroup" & i + 1
    .Caption = "Unit Group " & i + 1
End With


Set NewNumOfUnits = MultiPage1.Pages(1).Controls.Add("Forms.label.1")


With NewNumOfUnits
    .Width = 114
    .Top = 146
    .Left = 24
    .Height = 18
    .Width = 114
    .Name = "LBL_NumOfUnits" & i + 1
    .Caption = "Number of Units in Group:"
End With


Set NewLeaseTerm = MultiPage1.Pages(1).Controls.Add("Forms.label.1", "LBL_LeaseTerm2", True)


With NewLeaseTerm
    .Width = 102
    .Top = 168
    .Left = 24
    .Height = 18
    .Name = "LBL_LeaseTerm" & i + 1
    .Caption = "Lease Term in Months:"
    
End With


Set NewMthlyRent = MultiPage1.Pages(1).Controls.Add("Forms.label.1", "LBL_MonthlyRent2", True)


With NewMthlyRent
    .Width = 102
    .Top = 190
    .Left = 24
    .Height = 18
    .Name = "LBL_MonthlyRent" & i + 1
    .Caption = "Monthly Rent"
    
End With


'Draw the Text Boxes
Set NewNumberOfUnitsInGroupTxt = MultiPage1.Pages(1).Controls.Add("Forms.textbox.1")


With NewNumberOfUnitsInGroupTxt
    .Name = "txt_NumOfUnitsInGroup" & i + 1
    .Top = 141
    .Left = 156
    .MultiLine = False
    .EnterKeyBehavior = True
    .Height = 18
End With




Set NewLeaseTermTxt = MultiPage1.Pages(1).Controls.Add("Forms.textbox.1")


With NewLeaseTermTxt
    .Name = "txt_LeaseTerm" & i + 1
    .Top = 163
    .Left = 156
    .MultiLine = False
    .EnterKeyBehavior = True
    .Height = 18
End With

Set NewMonthlyRentTxt = MultiPage1.Pages(1).Controls.Add("Forms.textbox.1")


With NewMonthlyRentTxt
    .Name = "txt_MthlyRent" & i + 1
    .Top = 185
    .Left = 156
    .MultiLine = False
    .EnterKeyBehavior = True
    .Height = 18
End With

Case 2
' Draw the labels
Set NewUnitGrpLbl = MultiPage1.Pages(1).Controls.Add("Forms.label.1")


With NewUnitGrpLbl
    .Top = 218
    .Left = 18
    .Height = 18
    .Name = "LBL_UnitGroup" & i + 1
    .Caption = "Unit Group " & i + 1
End With


Set NewNumOfUnits = MultiPage1.Pages(1).Controls.Add("Forms.label.1")


With NewNumOfUnits
    .Width = 114
    .Top = 246
    .Left = 24
    .Height = 18
    .Width = 114
    .Name = "LBL_NumOfUnits" & i + 1
    .Caption = "Number of Units in Group:"
End With


Set NewLeaseTerm = MultiPage1.Pages(1).Controls.Add("Forms.label.1", "LBL_LeaseTerm2", True)


With NewLeaseTerm
    .Width = 102
    .Top = 268
    .Left = 24
    .Height = 18
    .Name = "LBL_LeaseTerm" & i + 1
    .Caption = "Lease Term in Months:"


End With


Set NewMthlyRent = MultiPage1.Pages(1).Controls.Add("Forms.label.1", "LBL_MonthlyRent2", True)


With NewMthlyRent
    .Width = 102
    .Top = 290
    .Left = 24
    .Height = 18
    .Name = "LBL_MonthlyRent" & i + 1
    .Caption = "Monthly Rent"


End With


'Draw the Text Boxes
Set NewNumberOfUnitsInGroupTxt = MultiPage1.Pages(1).Controls.Add("Forms.textbox.1")


With NewNumberOfUnitsInGroupTxt
    .Name = "txt_NumOfUnitsInGroup" & i + 1
    .Top = 241
    .Left = 156
    .MultiLine = False
    .EnterKeyBehavior = True
    .Height = 18
End With




Set NewLeaseTermTxt = MultiPage1.Pages(1).Controls.Add("Forms.textbox.1")


With NewLeaseTermTxt
    .Name = "txt_LeaseTerm" & i + 1
    .Top = 263
    .Left = 156
    .MultiLine = False
    .EnterKeyBehavior = True
    .Height = 18
End With




Set NewMonthlyRentTxt = MultiPage1.Pages(1).Controls.Add("Forms.textbox.1")


With NewMonthlyRentTxt
    .Name = "txt_MthlyRent" & i + 1
    .Top = 285
    .Left = 156
    .MultiLine = False
    .EnterKeyBehavior = True
    .Height = 18
End With


End Select
'BClicks  = BClicks  + 1
Next BClicks 
i = i + 1


End Sub
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,223,961
Messages
6,175,651
Members
452,664
Latest member
alpserbetli

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top