访问 VBA If 语句以更改报告字段的返回和字体颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19841554/
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 17:08:27  来源:igfitidea点击:

Access VBA If statement to Change Back and Font color of a report field

vbams-accessaccess-vba

提问by ackdaddy

I'm trying to make a field on a report highlight in red with white bold font on a report when it has an "S" populated. This keeps making all records in the field red. Please help!

我试图在报告上以红色突出显示一个字段,当它填充了“S”时,报告上的白色粗体字体。这会使字段中的所有记录保持红色。请帮忙!

Private Sub Report_Activate()
If Me![PULL STATUS] = "S" Then

Me![PULL STATUS].BackColor = vbRed
Me![PULL STATUS].FontBold = True
Me![PULL STATUS].ForeColor = vbWhite

End If
End Sub

采纳答案by Linger

The code you have should be contained in the On Formatevent of the Detailsection of the report. When you set the BackColor, FontBold, and ForeColorit stays that way until it is changed again.

您拥有的代码应该包含在 报告的“详细信息”部分的“格式”事件中。当您设置, 时,它会保持这种状态,直到再次更改为止。BackColorFontBoldForeColor

So what you need is an else statement to perform the opposite if not true. Something like:

所以你需要的是一个 else 语句,如果不是真的,则执行相反的操作。就像是:

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
  If Me![PULL STATUS] = "S" Then
    Me![PULL STATUS].BackColor = vbRed
    Me![PULL STATUS].FontBold = True
    Me![PULL STATUS].ForeColor = vbWhite
  Else
    Me![PULL STATUS].BackColor = vbWhite
    Me![PULL STATUS].FontBold = False
    Me![PULL STATUS].ForeColor = vbBlack
  End If
End Sub

回答by MSmith

MS Access uses Conditional Formatting - much like MS Excel; I recommend this instead of using VBA to change the backcolor pragmatically. This should work well for 'Continuous Forms.'

MS Access 使用条件格式——很像 MS Excel;我建议这样做而不是使用 VBA 以务实的方式更改背景色。这应该适用于“连续表格”。