更新后更改表单背景颜色的 VBA 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5422239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:16:49  来源:igfitidea点击:

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

ms-accessvbaaccess-vba

提问by Edmond

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

我需要一些代码,当未选中复选框时,它会更改表单的背景颜色,并在选中时将其恢复为原始颜色。The code i have for the check box currently locks a combo box when a value is chosen. 下面的例子

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

回答by Smandoli

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