VBA Userform disable and change backcolor of textboxes based on combo box selection

32 Views Asked by At

I have a userform with a combo box. When I select "Service" or "Repair" from the combo box dropdown, I want to disable and gray out several textboxes and combo boxes. When I select "System" I want them to revert to their original properties. I'm not sure how to do this. Plus, I'm getting syntax error on line arr = (txtQuoteApproved, etc...). Your help would be greatly appreciated:).

Private Sub cboOrderType_Change()
    Dim c As String
    Dim arr() As String
    c = Me.cboOrderType.Text
    arr = Array(txtApprovedQuote, txtPMAssign, txtSOATeam, txtFinance, txtApproved, txtSOACust, txtSOAE10, txtInvPaid, _
    txtShipReq, txtBOL, txtQuote, txtBMTH, txtTransOrdNum, txtDiamond, cboTE, txtPM, txtEE, cboSCodeDes, txtSCode, _
    txtSys, cboShipVia, cboShipType, cboShipChrgs, txtShipInst, txtCost, txtMargin, txtLT)
    If c = "Service" Then
            arr.Enabled = False 'Error: invalid qualifier on "arr"
            arr.BackColor = &HE0E0E0
    ElseIf c = "Repair" Then
            arr.Enabled = False
            arr.BackColor = &HE0E0E0
    ElseIf c = "System" Then
        'exit sub? How do I revert the array back to the original properties?
    End If
End Sub
1

There are 1 best solutions below

1
Tim Williams On BEST ANSWER

You need a loop if you want to update multiple controls

This worked for me:

Private Sub cboOrderType_Change()
    
    Dim arr, clr As Long, enable As Boolean
    
    arr = Array(TextBox1, TextBox2)
    
    Select Case Me.cboOrderType.Text
        Case "Service", "Repair"
            enable = False
            clr = vbGrayText
        Case "System"
            enable = True
            clr = vbWhite
        Case Else
            Exit Sub   'No match so just exit here, unless
                       '  there's some other fall-back required
    End Select
    
    For Each c In arr
        c.Enabled = enable
        c.BackColor = clr
    Next c
   
End Sub