I got the below code that is looping through all controls and changing the state in a after update event and enables new row for user to use. Everything works fine but i'm just wondering whether there is a better way to achieve the same result as what i have done is long winded. This will only enable the controls if the combobox has data if blank skip enable.
Private Sub Item1_AfterUpdate()
Call UpdateItemsToClean
End Sub
Private Sub UpdateItemsToClean()
Dim ctl As MSForms.Control
Dim lbl As MSForms.Label
Dim cmb As MSForms.ComboBox
Dim txtbox As MSForms.TextBox
Dim optbtn As MSForms.OptionButton
Dim i As Integer
For i = 2 To ItemsListFrame.Controls.Count
For Each ctl In ItemsListFrame.Controls
If TypeName(ctl) = "Label" Then
If ctl.Tag = "GroupItem" & i - 1 Then
Set lbl = ctl
If Controls("Item" & i - 1).Value <> vbNullString Then
Controls("OrderLbl" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "ComboBox" Then
If ctl.Tag = "GroupItem" & i - 1 Then
Set cmb = ctl
If Controls("Item" & i - 1).Value <> vbNullString Then
Controls("Item" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "TextBox" Then
If ctl.Tag = "GroupItem" & i - 1 Then
Set txtbox = ctl
If Controls("Item" & i - 1).Value <> vbNullString Then
Controls("Qty" & i).Enabled = True
Controls("UnitPrice" & i).Enabled = True
Controls("SubTotal" & i).Enabled = True
Controls("Comments" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "OptionButton" Then
If ctl.Tag = "GroupItem" & i - 1 Or ctl.Tag = "InOut" & i - 1 Then
Set optbtn = ctl
If Controls("Item" & i - 1).Value <> vbNullString Then
Controls("OptionIn" & i).Enabled = True
Controls("OptionOut" & i).Enabled = True
End If
End If
End If
Next ctl
Next i
End Sub
Subs
for each control type, e.g.UpdateLabelsToClean
andUpdateTextBoxesToClean
. That way your overall method won't get too long and hard to follow and may help if one set of controls needs additional logic. \$\endgroup\$