基于文本值输入的条件格式

时间:2018-07-18 02:07:31

标签: ms-access

我正在做一个表格,很难根据文本框输入突出显示字段。我无法突出显示其中包含许多信息的字段。

这是我的输入字段, I search for "Family"

这是我的字段,里面有多个值,以逗号分隔

I cannot highlight the entire field eventhough the word "family" is in that field

有人知道如何解决这个问题吗? 使用vba后的表格 I searched for "words", this is the first record, it works fine

Next record, there is no "words" in the field, but it still follow the previous rule from picture above

1 个答案:

答案 0 :(得分:1)

  1. 单击“搜索”按钮后,代码将检查文本框是否包含关键字。
  2. 然后循环浏览所有文本框,以查找具有匹配关键字的文本框并突出显示它们

Private Sub cmdSearch_Click()


    Dim ctrl As Control
    If Nz(Me.txtSearch, "") <> "" Then

        For Each ctrl In Me.Controls
            If TypeName(ctrl) = "TextBox" And ctrl.Name <> "txtSearch" Then

                If InStr(1, ctrl, Me.txtSearch, vbTextCompare) > 0 Then
                   ctrl.BackColor = vbRed
                Else
                      ctrl.BackColor = vbWhite
                End If
            End If
        Next

    End If

End Sub

要在移至下一条记录时重置文本框

    Private Sub Form_Current()
    Dim ctrl As Control
    'Me.txtSearch = ""

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
                  ctrl.BackColor = vbWhite
        End If
    Next
    cmdSearch_Click
End Sub

输出

enter image description here