I have a userform that creates a new multipage with each press of a button.
Each multipage will contain a number of dynamically created controls (textboxes, optionbuttons comboboxes etc)
Some of these dynamic controls must be Enabled/disabled via the selection or input values from other dynamically created controls.
So far here is my code for the project is as follows;
Option Explicit
Private x As Integer
Private Sub UserForm_Initialize()
x = 1
End Sub
Private Sub Cmd1_Click()
Dim OpPage As MSForms.Page
Dim Page1 As MSForms.MultiPage
Dim tankNameTxt As Control
Dim textYesNo1 As Control
Dim radioYes As Control
Dim radioNo As Control
Dim radio As Control
If x > 0 Then
Set OpPage = UserForm1.MultiPage2.Pages.Add("Tank Number " & x)
Set tankNameTxt = UserForm1.MultiPage2.Pages(x).Controls.Add("Forms.TextBox.1", "Tank" & x, True)
Set textYesNo1 = UserForm1.MultiPage2.Pages(x).Controls.Add("Forms.TextBox.1", True)
Set radioYes = UserForm1.MultiPage2.Pages(x).Controls.Add("Forms.OptionButton.1", "radioYes" & x, True)
Set radioNo = UserForm1.MultiPage2.Pages(x).Controls.Add("Forms.OptionButton.1", "radioNo" & x, True)
Set radio = UserForm1.MultiPage2.Pages(x).Controls.Add("Forms.OptionButton.1", "radio" & x, True)
With OpPage
.KeepScrollBarsVisible = 2
.ScrollBars = 2
.ScrollHeight = 500
End With
With tankNameTxt
.MultiLine = False
.WordWrap = True
.AutoSize = True
.ScrollBars = 2
.Left = 10
.Top = 15
.BorderStyle = 1
.SelectionMargin = False
.SpecialEffect = 0
.Height = 15
.Text = "Tank " & x & " Name"
End With
With radioYes
.Caption = "Is Yes"
.GroupName = "YesNo"
.Left = 10
.Top = 45
End With
With radioNo
.Caption = "Is No"
.GroupName = "YesNo"
.Left = 50
.Top = 45
End With
With textYesNo1
.Name = "textYesNo" & x
.BackStyle = 0
.BorderStyle = 0
.SpecialEffect = 0
.Value = "Yes No " & x
.Left = 3
.Locked = True
.Top = 35
.Height = 30
.Width = 80
End With
With radio
.Caption = "Is Disabled?"
.GroupName = "otherControl"
.Left = 50
.Top = 65
End With
'MsgBox "radio is " & radio.Name
x = x + 1
End If
End Sub
Private Sub radioYes_Click()
If UserForm1.MultiPage2.Pages(x).radioYes.Value = True Then
UserForm1.MultiPage2.Pages(x).radio.Enabled = True
ElseIf UserForm1.MultiPage2.Pages(x).radioNo.Value = True Then
UserForm1.MultiPage2.Pages(x).radio.Enabled = False
End If
End Sub
Problem
The iteration of x through the project and the use of integer x in the naming of the dynamically generated controls have me at a loss as to how to create code to affect the dynamically generated controls or even how to properly name the declarations for a Sub.
For instance
Private Sub radioYes_Click()
OR
Private Sub radioYes1_Click()
do not seem to work of have any effect on the controls I want to enable/disable.
I suspect this issue may be solved by the use of a Class but I frankly need help in understanding classes and their use in vba.
Any help on this would be appreciated.
Bookmarks