How to loop through all controls in a form, including controls in a subform – Access 2007

You can use recursion

Public Sub colCtrlReq(frm As Form)
''  Sets background color for required field -> Tag = *
Dim setColour As String
setColour = RGB(255, 244, 164)
Dim ctl As Control
For Each ctl In frm
        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox _
            Or ctl.ControlType = acListBox Then
            If InStr(1, ctl.Tag, "*") <> 0 Then
                ctl.BackColor = setColour
            End If
        ElseIf ctl.ControlType = acSubform Then
            colCtrlReq frm(ctl.Name).Form

        End If
Next ctl
Set ctl = Nothing
End Sub

Leave a Comment