Conditional Formatting with VBA

bwc

Registered User.
Local time
Yesterday, 23:14
Joined
Feb 7, 2013
Messages
25
I am trying to write a public function that will do conditional formatting, but I'm stuck. When I step through the code, it applys the correct forecolor, but to all the records; if the field is < then 0, all the records are red; if the field is >0, all the records are green...

Code:
Public Function redGreen(F As Form, fieldName As String)
    Dim rs As Recordset
    Set rs = F.Recordset
    
    rs.MoveFirst
    
        Do Until rs.EOF
            
                If F(fieldName).Value > 0 Then
                    F(fieldName).ForeColor = vbGreen
                ElseIf F(fieldName).Value < 0 Then
                    F(fieldName).ForeColor = vbRed
                End If
            
            rs.MoveNext
        Loop
    rs.Close
    Set rs = Nothing

End Function
 
Last edited:
I am assuming this is a conditional form and therefore this will not work. In order to get different conditions per record you have to use conditional formatting. That is not conditional formatting and every record will be one color, unless you use the paint event which can be problematic.
Use the conditional format gui to set your formatting rules for the control.

conf.png
 
If you need to set the rules dynamically during run time you would need to do something like this, but my guess you simply need to use the CF gui.
 

Users who are viewing this thread

Back
Top Bottom