Access FormatConditions Object For Each Iteration Is Incorrect

Software Bug

Have you ever needed to work with Conditional Formatting via VBA and tried to iterate through the FormatConditions collection?! Did you get incomplete results? Well, there is apparently a serious bug depending on the approach taken.

Note, this applies to Access 2016 and earlier, and per Karl’s comment below, has since been addressed in latter versions.

That being said, Microsoft did not push a fix for this bug to earlier versions. Thus, the workaround solution still remains the sole universal solution that will work properly on all versions of Access and as such, is the approach I recommend developers use.

The easiest approach to iterating over the collection is to do something like the following which is to iterate over the FormatConditions object:

Dim oFC As FormatCondition
Dim i As Long

For Each oFC In Me.FirstName.FormatConditions
    i = i + 1
    Debug.Print i, oFC.Expression1
Next oFC

Yet, this will fail to list more than 3 Format Conditions! Yep, you read that right. It will not necessarily list all the conditions. Making it worse, is I found no mention of this in the documentation (see below – maybe I missed it?!).

The Solution

Instead, the way to approach working with FormatConditions is to specifically use the Count property to iterate over the collection. So, you need to do something more like:

Dim i As Long

For i = 0 To Me.FirstName.FormatConditions.Count - 1
    Debug.Print i + 1, Me.FirstName.FormatConditions(i).Expression1
Next i

Case And Point

To illustrate this issue, I create a simple set of Conditional Formatting rules on a textbox and created the following:

Microsoft Access - Conditional Formatting Rules

And used both of the above code blocks to iterate over the conditional formatting rules to output some information to the immediate window.

Using the FormatConditions object approach, we get:

 1            "Marvin"
 2            "Joanne"
 3            "Larry"

Using the FormatConditions.Count approach, we get:

 1            "Marvin"
 2            "Joanne"
 3            "Larry"
 4            "Carrie"
 5            "Daniel"

Thus, as stated above, iterating over the FormatConditions object can possibly induce error by limiting the returned collection to only 3 rules (seems to be the 1st three rules that get returned)!

So just keep this in mind when programming as I was working on a project a short while ago and it took me a while to figure this out. I never thought that the issue was with the FormatConditions object and was sure it was my code. Yet, the inverse turned out to be true.

Thank you to Shane for mentioning this in a recent conversation as it refreshed my memory on the matter as I hadn’t made a blog posting at that time that I encountered this bug.
 

Additional Resources

4 responses on “Access FormatConditions Object For Each Iteration Is Incorrect

  1. Karl Donaubauer

    Daniel, you don’t mention the Access version where you tried this. The Access team fixed the previously in the FormatConditions collection not implemented change to more than 3 conditions in 2019 or 2020 for Access 365 and Access 2019, so almost 4 years ago. You shouldn’t see this problem any more with 2019, 2021 and 365.

    On the occasion of your article here I did a quick (re)check now with 365 (as I already had done in early 2020) and For Each delivered more than 3 conditions.

    1. Daniel Pineault Post author

      Indeed, you are correct. I had never heard of this issue before.

      I was working with Access 2013 at the time. The issue does seem to be fixed in latter versions, too bad they didn’t push the fix to earlier versions though.

  2. KSB

    Thanks. I had encountered this issue but could not find any solution on internet, and limited my format conditions to 3. Now I can increase the format conditions.