Access VBA If statement to Change Back and Font co

2019-07-10 11:16发布

问题:

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