ConditionalFormat in VBA (1 Viewer)

smig

Registered User.
Local time
Today, 17:15
Joined
Nov 25, 2009
Messages
2,209
I want to color the background of a text box, in a continuous form, based on the color codes stored in the text box itself.

I know ConditionalFormat is the only way, as this is a continuous form

How can it be done?
I want it to be done on the "FormOpen" event.

Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:15
Joined
Oct 29, 2018
Messages
21,474
How about this one? (Look towards the bottom.)

 

smig

Registered User.
Local time
Today, 17:15
Joined
Nov 25, 2009
Messages
2,209
How about this one? (Look towards the bottom.)

I think this one put me on the right track
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:15
Joined
Oct 29, 2018
Messages
21,474
Thanks

Is there any way to go over 3 Conditional formats (In VBA) ?
I think I read about it somewhere
If you have Access version 2010 and up, you can have up to 50 CFs.

Check out this demo.
 

smig

Registered User.
Local time
Today, 17:15
Joined
Nov 25, 2009
Messages
2,209
If you have Access version 2010 and up, you can have up to 50 CFs.

Check out this demo.
I still had error ""Error 7966. The format condition number you specified is greater than the number of format conditions."

So I looked for it and it seems Access can be real dumb :oops:
The error has nothing to do with the number of ConditionalFormats

This one won't work:
Code:
Dim strQRY As String
Dim i As Integer
Dim fc As FormatCondition

strQRY = "SELECT * FROM [11111] " & _
    "WHERE Not IsNoe([StatusColor]) "

Set rsStatus = CurrentDb.OpenRecordset(strQRY)

i = 0

With Me.StatusColor
    Me.StatusColor.FormatConditions.Delete
'    With rsStatus
        rsStatus.MoveFirst
        Do While Not rsStatus.EOF
            Set fc = Me.StatusColor.FormatConditions.Add(acExpression, acEqual, "[StatusColor] = " & rsStatus.Fields("StatusColor"), "")
            Set fc = Me.StatusColor.FormatConditions(i)
            fc.BackColor = Val(rsStatus.Fields("StatusColor"))
            i = i + 1
            rsStatus.MoveNext
        Loop
        rsStatus.Close
'    End With
End With

Set fc = Nothing

but using LIKE will work :rolleyes:
Code:
Dim strQRY As String
Dim i As Integer
Dim fc As FormatCondition

strQRY = "SELECT * FROM [11111] " & _
    "WHERE Not IsNoe([StatusColor]) "

Set rsStatus = CurrentDb.OpenRecordset(strQRY)

i = 0

With Me.StatusColor
    Me.StatusColor.FormatConditions.Delete
'    With rsStatus
        rsStatus.MoveFirst
        Do While Not rsStatus.EOF
            Set fc = Me.StatusColor.FormatConditions.Add(acExpression, acEqual, "[StatusColor] like " & rsStatus.Fields("StatusColor"), "")
            Set fc = Me.StatusColor.FormatConditions(i)
            fc.BackColor = Val(rsStatus.Fields("StatusColor"))
            i = i + 1
            rsStatus.MoveNext
        Loop
        rsStatus.Close
'    End With
End With

Set fc = Nothing
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:15
Joined
May 7, 2009
Messages
19,245
you can use the Paint event of detail section?
 

Attachments

  • RGB_COLOR.accdb
    556 KB · Views: 260

isladogs

MVP / VIP
Local time
Today, 15:15
Joined
Jan 14, 2017
Messages
18,236
Did you try Royce's solution in the first Post of the link given by @theDBguy in post #10?
I know that works and allows you to bypass the 3 CF limit that still exists using code.
Otherwise, use the CF wizard which allows up to 50 conditions.
 

smig

Registered User.
Local time
Today, 17:15
Joined
Nov 25, 2009
Messages
2,209
Did you try Royce's solution in the first Post of the link given by @theDBguy in post #10?
I know that works and allows you to bypass the 3 CF limit that still exists using code.
Otherwise, use the CF wizard which allows up to 50 conditions.
As I said in #11, I managed to overcome the 3 cf limit.
Using LIKE instead of "=" solved the problem :unsure:
It seems it has nothing to do with the number of cf
 

isladogs

MVP / VIP
Local time
Today, 15:15
Joined
Jan 14, 2017
Messages
18,236
As I said in #11, I managed to overcome the 3 cf limit.
Using LIKE instead of "=" solved the problem :unsure:
It seems it has nothing to do with the number of cf
Yes I was aware you had found a workround.
My question was whether you had tried Royce's solution
 

smig

Registered User.
Local time
Today, 17:15
Joined
Nov 25, 2009
Messages
2,209
Yes I was aware you had found a workround.
My question was whether you had tried Royce's solution
No I did not see it before, so i didnt try before.

Now i tried it and found it to be the most mystery bug I ever saw 🤣
If i start on 3, as suggested it wont even work with LIKE.
It will error on i = 3

But why do we need the cf at all, if we can simply use the Paint event ?
 

isladogs

MVP / VIP
Local time
Today, 15:15
Joined
Jan 14, 2017
Messages
18,236
Try reverting to = and then start at i=3
Or just use the wizard which allows up to 50 CF conditions

I doubt you can do everything that is possible with CF using the Paint event. Or at least, not do so as easily as with CF
 

smig

Registered User.
Local time
Today, 17:15
Joined
Nov 25, 2009
Messages
2,209
Try reverting to = and then start at i=3
Or just use the wizard which allows up to 50 CF conditions

I doubt you can do everything that is possible with CF using the Paint event. Or at least, not do so as easily as with CF
I tried reversing to =
Didnt work. Wont even work for LIKE.
Actually it will error for the first fc (i=3).
This realy put me into the conclusion that this error has nothing to do with the number of the fc perse

In this situation using the wizard cant be done, as i want to let the end user set the bg color.

In any case i have something that work now, and i can also easily change to paint event, as all i need is painting the bg
 
Last edited:

Adelina_RO

Member
Local time
Today, 17:15
Joined
Apr 9, 2021
Messages
42
Two year later with a solution for a 10 years old problem:
don't add space between the expression elements.
Code:
For I = 1 To 10
    With Telefon.FormatConditions.Add(acExpression, acBetween, "1=1")
        .FontBold = True
    End With
Next
--> works perfectly;

Code:
For I = 1 To 10
    With Telefonc.FormatConditions.Add(acExpression, acBetween, "1 = 1")
        .FontBold = True
    End With
Next
--> throws the infamous error.
I mean, the solution with the three preceding conditions works, but if you need to constantly update FC on multiple fields, that becomes an issue, especially with more than 3 different controls on a form 1920x1080.
 

Users who are viewing this thread

Top Bottom