简体   繁体   中英

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

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

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 ForeColor it 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM