【问题标题】:Reporting on formatting VBA code报告格式化 VBA 代码
【发布时间】:2016-10-16 14:20:27
【问题描述】:

我有一个包含步骤列表文本框的报告。我需要根据基于查询的特定条件使用 VBA 代码调整它的背景颜色。我已经在详细信息面板的“On Format”下设置了代码。如果我调出一个步骤,它将改变步骤的颜色。如果我调用多个步骤,它只会突出显示最后一个框。对于测试,我有一个包含 13 个步骤的报告,但我想更改第 1 步和第 4 步。这是我使用的代码:

If DCount("*", "qry_step_check1") = 1 And Reports![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].Value = 1 Then
    [Reports]![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].BackColor = lngYellow
Else
    [Reports]![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].BackColor = lngWhite
End If

If DCount("*", "qry_step_check4") = 1 And Reports![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].Value = 4 Then
        [Reports]![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].BackColor = lngYellow
Else
        [Reports]![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].BackColor = lngWhite
End If

在这种情况下,只有第 4 步发生了变化。如果我删除第 4 步的列表,第 1 步将突出显示。在这一点上,我不确定我正在做的事情是否有效,或者我只是错过了一些简单的事情。我也尝试过使用 DLookups 并获得相同的结果。我知道代码可能不是最有效的,但在这一点上,我只是想让它发挥作用,以后可以担心更高效的代码。这是我正在使用的设计视图的快速截图。

【问题讨论】:

    标签: ms-access vba ms-access-2010 ms-access-2013


    【解决方案1】:

    经过多次戳戳和刺激,我找到了一个解决方案,可能不是最干净的,但它可以满足我的需要。幸运的是,它并没有过多地拖慢报告速度。

    Dim lngRed As Long, lngYellow As Long, lngWhite As Long
    Dim i As Integer
    lngRed = RGB(255, 0, 0)
     lngBlack = RGB(0, 0, 0)
     lngYellow = RGB(255, 255, 0)
     lngWhite = RGB(255, 255, 255)
     i = 1
    
    Do Until i = 75
    
    If Reports![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].Value = i Then
    If DLookup("[Step_Int]", "[qry_step_check]", "[Step_Int] = " & i) = i Then
    [Reports]![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].BackColor = lngYellow
    Else
    [Reports]![rpt_WI_BOOK]![qry_Select_Step_Filter_Task].Report![Step].BackColor = lngWhite
    End If
    End If
    i = i + 1
    Loop
    

    使用此代码几天后,确定在大型报告中,它确实减慢了太多。我最终删除了代码并在“步骤”框中设置了条件格式,现在它的处理速度提高了大约 4 倍。 这是条件格式中使用的表达式:

    DLookUp("[Step_Int]","[qry_step_check1]","[Step_Int] = Reports!rpt_WI_Book!qry_select_step_filter_task.Report!Step.Value")=[Reports]![rpt_WI_Book]![qry_Select_Step_Filter_Task].[Report]![Step].[Value] And DLookUp("[Task_ID]","[qry_step_check1]","[Task_ID] = Reports!rpt_WI_Book!Text474.Value")=[Reports]![rpt_WI_Book].[Report]![Text474].[Value]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 2014-09-26
      • 1970-01-01
      相关资源
      最近更新 更多