Search code examples
vbams-accessconditional-formatting

MS Access VBA to see if a conditional formatting rule is currently active for a control


I have a conditional formatting rule that enables some fields depending on choices earlier. The fields are disabled by default. Once these fields are enabled, they also require input. However since they are conditional I cannot make them required on the database level.

What I've tried is to check in the submit handler if the control is enabled and not empty.

Public Sub SaveButton_Click()
    For Each ctl In Me.Controls
        If (ctl.Tag = "ConditinallyRequiredField") Then
            If (ctl.Enabled = True) Then
                Debug.Print "This is never reached"
                ' Check for empty values.
                If (Len(ctl.Value & vbNullString) = 0) Then
                    MsgBox "One or more required fields are missing input values."
                    GoTo stopSubmit
                End If
            End If
        End If
    Next ctl

    ... Do submit
End Sub

But ctl.Enabled is always false, no matter at what point in time I check it. So it looks like the conditional formatting overrides it without affecting the actual property.

So since ctl.Enabled apparently always remains false, I would like to check if there is a conditional formatting rule active for the given control (there is only one). But so far I have not found to do so. Can it be done?


Solution

  • Due to dynamic nature of CF, I doubt can determine if the conditional format is applied at any particular time. None of the CF properties/methods offer this indicator. Most likely why there is no example code to be found. The same condition used in CF rule to enable/disable can be used in VBA to determine control's state.

    Can determine if control has any CF rules.
    ctl.FormatConditions.Count
    Only textboxes and comboboxes can have CF so make sure loop code only tests those controls. Anything else will trigger a "doesn't support this property" error.