If command to produce different colored font upon certain criteria?

pam0566

Registered User.
Local time
Today, 03:17
Joined
Jun 6, 2008
Messages
132
Is there a command or code that will produce different color font if the field contains certain criteria? For instance, if the AgentID field contains the words anywhere in there "NEED AGT" then the form will display that field writing in red? to stand out? I tried to play with some commands but nothing worked on it. ANy ideas?
 
Look at Conditional Formatting (Format/Conditional Formatting).
 
Is this a Single View, Continuous View or Datasheet View form?
 
This will do it. Just replace YourFieldName with the actual name of your field.

Code:
Private Sub YourFieldName_AfterUpdate()
 If InStr(Me.YourFieldName, "NEED AGT") > 0 Then YourFieldName.ForeColor = vbRed
End Sub

Private Sub Form_Current()
 If InStr(Me.YourFieldName, "NEED AGT") > 0 Then
  YourFieldName.ForeColor = vbRed
 Else
  YourFieldName.ForeColor = vbBlack
 End If
End Sub
 
I'm curious why you would put code in 2 events when Conditional Formatting would do it all for you, and will work regardless of what view the form is in.
 
I personally wouldn't, but

  • A lot of people aren't comfortable with using Conditional Formatting
  • A lot of people are still running pre-2000 versions of Access
  • While this task can be done using Conditional Formatting, a lot of formatting jobs can't
  • Newbies need to get used to using VBA
 
I am trying the conditional formatting, but i cant seem to get the right expression to do what i needed , i tried: [cboagent]= like "*" & "need agt" & "*" but that gave me a synax error-- any ideas on where the expression is wrong? thats the expression i use to generrate reports with the same info?
 
As answered elsewhere, drop the "=".
 

Users who are viewing this thread

Back
Top Bottom