Conditional formatting by group (1 Viewer)

Gismo

Registered User.
Local time
Today, 02:24
Joined
Jun 12, 2017
Messages
1,298
Hi Al,

I have a form with registration and inspection
Each registration number could have multiple inspections
Form is sorted by registration.
I want to highlight all the registrations to group them together, each registration number with a different color.
is there a way to accomplish this as per attached sample?
Access can determine the color of each registration number
Capture.JPG
 

June7

AWF VIP
Local time
Yesterday, 15:24
Joined
Mar 9, 2014
Messages
5,463
How many registration codes are there?

Use Conditional Formatting.
 

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
You will need to specify the colours for each code using the conditional formatting wizard.
I believe you can have up to 50 conditions.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:24
Joined
May 7, 2009
Messages
19,227
see form tlRegistration.
see how fncColorize is being called on the form's open event.
see the code and its limitation.
 

Attachments

  • sampleGroupColor.zip
    24.9 KB · Views: 44

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
@Arnel
I had also considered a similar solution though with slightly different code.

Tested yours by adding two additional records with different values.
Obviously no colours added as the code is in form open event.
Closed and reopened the form. Still just the same three colours added to the first three registration codes with other records not coloured.
Think it needs a bit more work.
 

Gismo

Registered User.
Local time
Today, 02:24
Joined
Jun 12, 2017
Messages
1,298
I have about 40 records in the table, with the inspections its about 200 records combined
I tried the code but all blank instead of the first 3 colors
 

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
Thanks for confirming. Same issue as I had. Hopefully arnel will fix it though you will still have repeated colours as he stated

A variation which I also considered would be to adapt this form that I use for calendar events:



The colour values are stored in a separate table tblCategories
Replace the Category field with Registration & scrap the CatShort column.
Add as many colours as needed.
Then as new registration codes are added, assign the registration code to a blank record.

The question is whether its worth doing compared to just using the CF wizard
 

Attachments

  • EditCalendarCategories.gif
    EditCalendarCategories.gif
    38.9 KB · Views: 125

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:24
Joined
May 7, 2009
Messages
19,227
this is more generic:
Code:
Option Compare Database
Option Explicit


Public Function fncColorize(ByRef ctl As TextBox)
    '*
    '* arnelgp
    '*
    '*NOTE:
    '*
    '* there is limit to the number of FormatConditions you can set.
    '* some say its limited between 32 to 50
    '*
    '* so if you have more than 32 distinct Registration, this will fail.
    '*
    '*
    '* create as many color if you have manny different Registration
    '* or you can recycle the color every four registration
    Const LIMITS As Integer = 32
    
    Dim arrcolor
    Dim colCondition As Collection
    Dim bolNew As Boolean
    Dim i As Integer, j As Integer
    
    '* three colors at the moment but you can add more
    arrcolor = Array(11957550, 3243501, 49407)
    
    '* delete all format conditions
    ctl.FormatConditions.Delete
    '* new collection
    Set colCondition = New Collection
    i = 1
    bolNew = True
    '* open recordset
    With CurrentDb.OpenRecordset( _
        "SELECT DISTINCT Registration From tblRegistration ORDER By Registration ASC;", _
        dbOpenSnapshot)
        If Not (.BOF And .EOF) Then .MoveFirst
        On Error Resume Next
        While Not .EOF
            '* limit the conditions to 32
            '* so on the  33 records, the color will go back to color 1 of the array
            If i > LIMITS Then
                i = 1
                bolNew = False
            End If
            If bolNew Then
                colCondition.Add !Registration & "/", i & ""
                
            Else
                colCondition.Item(i) = colCondition.Item(i) & !Registration & "/"
                
            End If
            i = i + 1
            .MoveNext
        Wend
    End With
    j = 0
    For i = 1 To colCondition.Count
        '* add the FormatCondition with background color
        With ctl.FormatConditions.Add(AcFormatConditionType.acExpression, _
                    acEqual, _
                    "Instr(""" & colCondition.Item(i) & """,[Registration])>0")
                .BackColor = arrcolor(j)
        End With
        j = j + 1
        If j > UBound(arrcolor) Then j = 0
    Next i
    Set colCondition = Nothing
End Function

ridders:

you have to call the functiin again on yhe forms afterupdate event.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:24
Joined
May 7, 2009
Messages
19,227
can you post db wth dummy data but real table and form.
 

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
you have to call the functiin again on yhe forms afterupdate event.

I tried using both the form_afterupdate & form_current events before I posted.
NEITHER worked. I even tried adding colour values instead of vbBlue etc.
Still just three colours.

UPDATE:
Just checked - the 3 colours are being applied by conditional formatting presumably done by the OP
Removed that & there are no colours.
It seems your fncColorize doesn't do anything in my tests
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:24
Joined
May 7, 2009
Messages
19,227
cant believe you cant get it work.
 

Attachments

  • sampleGroupColor.zip
    34.5 KB · Views: 33

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
It didn't work for the OP or me.

However the latest version does now work as it should on both forms apart from a brief loss of colour after adding a new record.
I tested it by deleting all the CF conditions using the wizard and all were restored on reopening

I see you've now replaced vbBlue etc with colour values. Perhaps that helped?
AFAIK you can have up to 50 different colour formats usng CF.

Rather than hardcoding the values in the function, I prefer to use a separate table/form as in my earlier post so users can easily modify the colours .... but the idea is very similar.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:24
Joined
May 7, 2009
Messages
19,227
this is an updated ver. it uses color from table webcolor.
there is also a sample form to view and delete the color the op want.
 

Attachments

  • sampleGroupColor.zip
    45.3 KB · Views: 32

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
Arnel
Yup. That's almost exactly what I was suggesting in my last post

Can I make one further suggestion you may wish to include:
Currently its working through the colours in order which means adjacent groups are fairly similar to each other

So I suggest you:
EITHER randomise the colours in the webcolor table
OR (probably better) randomise the selection taken from the table
 

Gismo

Registered User.
Local time
Today, 02:24
Joined
Jun 12, 2017
Messages
1,298
Works like a charm
Thank you very much
only thing is, some colors are very close, how do we randomize the colors selected from webcolor?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:24
Joined
May 7, 2009
Messages
19,227
you can add another field (long) to webcolor.
reate a quer to update the field:

update webcolor set newfield=(rnd*134)+1 ;

on the code use the newfield as sort order.
 

isladogs

MVP / VIP
Local time
Today, 00:24
Joined
Jan 14, 2017
Messages
18,209
you can add another field (long) to webcolor.
reate a quer to update the field:

update webcolor set newfield=(rnd*134)+1 ;

on the code use the newfield as sort order.

@arnelgp
If that's an answer to my comment, I knew how to alter it.
However, I didn't feel it was appropriate for me to post an alteration to your example
 

Gismo

Registered User.
Local time
Today, 02:24
Joined
Jun 12, 2017
Messages
1,298
Hi,

For some reason, i only see the color once I click on the field, same with a different field which has conditional formatting for a negative value. This did not happen yesterday.
Any suggestion why this happens?
 

Users who are viewing this thread

Top Bottom