2011-03-24 69 views
1

我需要一些代码,当未选中某个复选框时,它将更改我窗体的背景颜色,并在选中时将其恢复为其原始颜色。当我选择一个值时,我为复选框选择的代码当前锁定了一个组合框。下面更新后更改窗体背景颜色的VBA代码

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 

回答

5

在标准模块实施例(未形式模块 - 常数的范围将仅限于形成,因而你将无法再使用它们):

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

现在在表格的模块中:

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 
+0

谢谢,但你能告诉我在哪里Public Const colorBlue ....会去我的代码? – Edmond 2011-03-25 15:15:55

+0

它只需要进入标准模块的标题区域。不是表单模块,我很抱歉没有明确说明。分配常量只是一种方便,但是由于我将这种更改应用于其他表单,实际上它不仅仅是方便。您可能需要对“subFrm”进行一些说明。以及... – Smandoli 2011-03-25 15:20:50