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

馋奶兔 提交于 2019-12-24 04:53:17

问题


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!

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

回答1:


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

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

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



回答2:


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.'



来源:https://stackoverflow.com/questions/19841554/access-vba-if-statement-to-change-back-and-font-color-of-a-report-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!