VBA code that changes the background color of a fo

2019-08-05 01:03发布

问题:

I need some code that when a check box is unchecked it will change the background color of my form and return it back to its original color when checked. The code i have for the check box currently locks a combo box when a value is chosen. Example below

Private Sub AccessKeyNo_AfterUpdate()
If MsgBox("Do you want to assign Access Key " & Me.AccessKeyNo & "?", _
        vbYesNo) = vbYes Then
    Me.GuestAccessKeyID = Me.AccessKeyNo

    If Me.Dirty Then Me.Dirty = False
    Me.AccessKeyNo.Requery
    Me.AccessKeyNo = Null

    Me.MyCheckBox = IsNull(Me.GuestAccessKeyID)
End If
End Sub

回答1:

In a standard module (not the form module -- the scope of the constants would be limited to form, thus you wouldn't be able to reuse them):

Public Const colorBlue_Cornflower = "15714765"
Public Const colorTan_Encarnacion = "11398133"

Now in the module for the form:

Dim colorThis as String, booWhatever as Boolean

booWhatever = Me.MyCheckBox  ''Use of the variable can prevent problems

If booWhatever Then
     colorThis = colorBlue_Cornflower
Else
     colorThis = colorTan_Encarnacion 
End If

subFrm.Form.Section(acDetail).BackColor = colorThis 
subFrm.Form.Section(acHeader).BackColor = colorThis 

subFrm.Form.Repaint