move through controls in form

Hi,
How can i go through each control in form and run conditions if the control is textbox or combobox and the backcolor of the controls are #F9EDED ,my code is not working:

Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control

    nl = vbNewLine & vbNewLine

    For Each ctl In Me.Controls
      If ctl.ControlType = acTextBox or acComboBox Then
        If ctl.backcolor="#F9EDED" And Trim(ctl & "") = "" Then
         msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
               "You can't save this record until this data is provided!" & nl & _
               "Enter the data and try again . . . "
          Style = vbCritical + vbOKOnly
          Title = "Required Data..."
          MsgBox msg, Style, Title
          ctl.SetFocus
          Cancel = True
          Exit For
        End If
      End If
    Next

End Sub

Open in new window

drtopservAsked:
Who is Participating?
 
drtopservConnect With a Mentor Author Commented:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control

    nl = vbNewLine & vbNewLine

    For Each ctl In Me.Controls
      If ctl.ControlType = acTextBox or ctl.ControlType = acComboBox Then
        If ctl.backcolor=RGB(249,237,237) And Trim(ctl & "") = "" Then
         msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
               "You can't save this record until this data is provided!" & nl & _
               "Enter the data and try again . . . "
          Style = vbCritical + vbOKOnly
          Title = "Required Data..."
          MsgBox msg, Style, Title
          ctl.SetFocus
          Cancel = True
          Exit For
        End If
      End If
    Next

End Sub
0
 
drtopservAuthor Commented:
i think i have solved the problem myself:}
thnx anyway:}
0
 
GrahamSkanRetiredCommented:
You can use this notation if you know the colour in hex

If ctl.backcolor=&HF9EDED And Trim(ctl & "") = "" Then

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.