VBA code that changes the background color of a form after update

拈花ヽ惹草 提交于 2019-12-24 09:38:42

问题


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


来源:https://stackoverflow.com/questions/5422239/vba-code-that-changes-the-background-color-of-a-form-after-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!