MS Access 2007中的条件格式在VBA多个文本框“处理过大”

问题描述 投票:0回答:2

我有有一排73个文本框的报告,每个被拉升的记录将是9的一个结果,我需要根据结果对颜色的每个框。我开始把在一起的。如果再Else语句引用每箱所以我做到了

    If Me!A = "0-1" Then
    Me!A.ForeColor = 329171
    Me!A.BackColor = 329171
    ElseIf Me!A = "1-2" Then
    Me!A.ForeColor = 33023
    Me!A.BackColor = 33023
    ElseIf Me!A = "2-3" Then
    Me!A.ForeColor = 251574
    Me!A.BackColor = 251574
    ElseIf Me!A = "3-4" Then
    Me!A.ForeColor = 16645487
    Me!A.BackColor = 16645487
    ElseIf Me!A = "4-5" Then
    Me!A.ForeColor = 8453888
    Me!A.BackColor = 8453888
    ElseIf Me!A = "5-6" Then
    Me!A.ForeColor = 12615680
    Me!A.BackColor = 12615680
    ElseIf Me!A = "6-7" Then
    Me!A.ForeColor = 16744703
    Me!A.BackColor = 16744703
    ElseIf Me!A = "7-8" Then
    Me!A.ForeColor = 65535
    Me!A.BackColor = 65535
    ElseIf Me!A = "8-9" Then
    Me!A.ForeColor = 32896
    Me!A.BackColor = 32896
    Else
    Me!A.ForeColor = 0
    Me!A.BackColor = 16777215

    End If

    If Me!B = "0-1" Then
    Me!B.ForeColor = 329171
    Me!B.BackColor = 329171
    ElseIf Me!B = "1-2" Then
    Me!B.ForeColor = 33023
    Me!B.BackColor = 33023
    ElseIf Me!B = "2-3" Then
    Me!B.ForeColor = 251574
    Me!B.BackColor = 251574
    ElseIf Me!B = "3-4" Then
    Me!B.ForeColor = 16645487
    Me!B.BackColor = 16645487
    ElseIf Me!B = "4-5" Then
    Me!B.ForeColor = 8453888
    Me!B.BackColor = 8453888
    ElseIf Me!B = "5-6" Then
    Me!B.ForeColor = 12615680
    Me!B.BackColor = 12615680
    ElseIf Me!B = "6-7" Then
    Me!B.ForeColor = 16744703
    Me!B.BackColor = 16744703
    ElseIf Me!B = "7-8" Then
    Me!B.ForeColor = 65535
    Me!B.BackColor = 65535
    ElseIf Me!B = "8-9" Then
    Me!B.ForeColor = 32896
    Me!B.BackColor = 32896
    Else
    Me!B.ForeColor = 0
    Me!B.BackColor = 16777215

    End If 

等,为每个73个箱,然后我得到的消息程序太大。显然,是在我的代码有很多重复的 - 我完全是新的这 - 所以我想知道是否有一种方法可以一次使用的代码,但参考所有的73个箱?

ms-access-2007 access-vba
2个回答
1
投票

存储查找对数据表中,这样的事情:

KeyValue   Foreground   Background
0-1          329171      329171
1-2           33023       33023
2-3          251574      251574
3-4        16645487    16645487
4-5         8453888     8453888
5-6        12615680    12615680
6-7        16744703    16744703
7-8           65535       65535
8-9           32896       32896

当然,现在我已经在作为例子编辑这个数据,我注意到,前景和背景的颜色是相同的,但并没有真正影响我的答案。

现在,在你的代码,而不是一个的if / then / else语句作为测试,以确定颜色,你会使用一个使用DLookup()来查找基于你的测试值的颜色:

  Dim lngForeColor As Long
  Dim lngBackColor As Long

  lngForeColor = DLookup("Foreground", "tblColors", "[KeyValue]='" & Me!A & "'")
  lngBackColor = DLookup("Background", "tblColors", "[KeyValue]='" & Me!A & "'")
  Me!A.ForeColor = lngForeColor
  Me!A.BackColor = lngBackColor

现在,亚伦的建议,通过控制回路的改进结合这样的:

  Dim ctl As Control
  Dim lngForeColor As Long
  Dim lngBackColor As Long

  For Each ctl in Me.Detail.Controls
    If DCount("*", "tblColors", "[KeyValue]='" & ctl.Value & "'") = 0 Then
       lngForeColor = 0
       lngBackColor = 16777215
    Else
       lngForeColor = DLookup("Foreground", "tblColors", "[KeyValue]='" & ctl.Value & "'")
       lngBackColor = DLookup("Background", "tblColors", "[KeyValue]='" & ctl.Value & "'")
    End If
    ctl.ForeColor = lngForeColor
    ctl.BackColor = lngBackColor
  Next ctl
  Set ctl = Nothing

如果您的前景和背景颜色都一样,你就不需要在你的表两列,而你只需要做一个查找。

现在,如果你不改变所有的控制,只是其中的一部分,你可以使用控件的.TAG属性,以使该条件:

  For Each ctl in Me.Detail.Controls
    If .Tag = "ColorGroup" Then
       ' set the colors
    End If
  Next ctl

当你需要改变一组控件有条件地从记录的数据更好的建议是创建一个自定义的收集和表单报表的OnOpen事件指派那些控制它。要做到这一点,你会创建一个类型集合的模块级变量:

  Dim colColorGroup As New Collection

在报告的OnOpen事件,你可以这样做:

  Dim ctl As Control

  For Each ctl in Me.Detail.Controls
    colColorGroup.Add ctl, ctl.Name   
  Next ctl
  Set ctl = Nothing

那么在细节的Format事件,而不是走的控制整个集团,你通过这个集合倒是循环:

  Dim varItem As Variant
  Dim ctl As Control

  For Each varItem in colColorGroup
    Set ctl = varItem
    If DCount("*", "tblColors", "[KeyValue]='" & ctl.Value & "'") = 0 Then
       lngForeColor = 0
       lngBackColor = 16777215
    Else
       lngForeColor = DLookup("Foreground", "tblColors", "[KeyValue]='" & ctl.Value & "'")
       lngBackColor = DLookup("Background", "tblColors", "[KeyValue]='" & ctl.Value & "'")
    End If
    ctl.ForeColor = lngForeColor
    ctl.BackColor = lngBackColor
  Next varItem
  Set ctl = Nothing

这将是比通过较大的组控件循环和基于Tag属性选择快得多。


1
投票
Dim ctl as control

for each ctl in me.controls
    If me.controls(ctl.name).tag = "X" then me.controls(ctl.name).backcolor = ""
next ctl
© www.soinside.com 2019 - 2024. All rights reserved.