access 批量设置条件格式
Private Sub Form_Load() For Each temp In Me.Controls If Not TypeOf temp Is Label Then temp.OnClick = "=GetVal()" Next If Me.产品编码.FormatConditions.Count = 0 Then AddConditionalFormattingToFields '最好加个IF,不然每次保存窗体,都会把条件格式保留下来,性能变差 End Sub Function GetVal() Me.Tag = Nz(Me.单据编号, "") '设置窗体的tag值,以便在条件格式的表达式中能够引用得到这个值 Me.Refresh End Function Sub AddConditionalFormattingToFields()
Dim ctl As Control Dim str As String ' 遍历窗体的每个控件 For Each ctl In Me.Controls ' 只处理文本框类型的控件(包括文本框、组合框等) If ctl.ControlType = acTextBox Then ' 构建条件格式的表达式 str = "[单据编号]=[Tag]" ' 添加条件格式到当前控件 ctl.FormatConditions.Add acExpression, , str '注意这里 ctl.FormatConditions(ctl.FormatConditions.Count - 1).BackColor = RGB(239, 183, 0) ' 设置背景色 End If Next ctl End Sub
这样就省下了不少功夫.效果如下.这个效果和你一个字段一个字段的手动添加条件格式,其实是一样的