根据子表单上文本框中键入的内容过滤子表单

发布于 2024-09-27 03:59:36 字数 251 浏览 3 评论 0原文

我正在尝试根据子表单上的文本框中键入的内容来过滤子表单上显示的记录。该子表单称为“用户”,位于“组”表单上。在“用户”子表单上有一个名为 txtFilter 的文本框。如果我在 txtFilter 中输入“W”,我只想显示用户的姓氏或用户名以“W”开头的记录。当我继续输入 W...A...LI 时,只希望显示姓氏或用户名以“Wal”开头的用户。

我有一些模糊的想法,我必须使用记录集属性或子表单的 serverFilter 来执行此操作,但我真的不知道该怎么做。请帮我!

I'm trying to filter the records shown on a subform based on what is typed in a textbox on the subform. The subform is called Users, and it is on the Group form. On the Users subform there is a textbox called txtFilter. If I type "W" in txtFilter, I want to show only records in which the User's lastName or userName begins with a "W". As I continue typing W...A...L I want only Users whose lastName or UserName begin with "Wal" to show up.

I have some vague idea that I have to use either the recordset property or the subform's serverFilter to do this, but I'm really at a loss as to what to do. Please help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小草泠泠 2024-10-04 03:59:36
Private Sub txtFilter_Change()

    If Nz(Me.txtFilter.Text, "") = "" Then
        Me.FilterOn = False
        Me.txtFilter.SetFocus
        Exit Sub
    End If
    Me.Filter = "lastName like '" + Me.txtFilter.Text + "%' or userName like '" & _
        Me.txtFilter.Text + "%'"
    Me.FilterOn = True
    Me.txtFilter.SetFocus
    Me.txtFilter.SelStart = Len(Nz(Me.txtFilter.Text, "")) + 1


End Sub
Private Sub txtFilter_Change()

    If Nz(Me.txtFilter.Text, "") = "" Then
        Me.FilterOn = False
        Me.txtFilter.SetFocus
        Exit Sub
    End If
    Me.Filter = "lastName like '" + Me.txtFilter.Text + "%' or userName like '" & _
        Me.txtFilter.Text + "%'"
    Me.FilterOn = True
    Me.txtFilter.SetFocus
    Me.txtFilter.SelStart = Len(Nz(Me.txtFilter.Text, "")) + 1


End Sub
○愚か者の日 2024-10-04 03:59:36

快速更新 Access 2013 的 @dmr 响应:

  • 将“%”更改为“*” - Access 使用星号作为通配符。除了在文本之后之外,可能还想将其添加在文本之前。
  • 您需要在表单属性中打开AllowAdditions,否则当您的过滤器返回零命中时,您将出现焦点错误。
  • 此代码将修剪空格,这使得无法在搜索中添加空格(因为如果不先输入“两个”,则无法输入“两个单词”)。

修改后的响应:

Private Sub txtFilter_Change()

    Dim search_text As String
    search_text = Me.txtFilter

    If Nz(Me.txtFilter.Text, "") = "" Then
        Me.FilterOn = False
        Me.txtFilter.SetFocus
        Exit Sub
    End If
    Me.Filter = "lastName like '*" + Me.txtFilter.Text + "*' or userName like '*" & _
        Me.txtFilter.Text + "*'"
    Me.FilterOn = True
    Me.txtFilter.SetFocus
    Me.txtFilter.Value = search_text
    Me.txtFilter.SelStart = Len(Nz(Me.txtFilter.Text, "")) + 1


End Sub

Quick update to @dmr response for Access 2013:

  • Change "%' to "*' - Access uses the asterisk for wildcard. Might want to add it before the text, in addition to after.
  • You'll want to turn ON AllowAdditions in the form properties, or you'll have focus errors when your filter returns zero hits.
  • This code will trim whitespaces, which makes it impossible to add spaces to your search (since you can't type 'two words' without first typing 'two ').

Modified response:

Private Sub txtFilter_Change()

    Dim search_text As String
    search_text = Me.txtFilter

    If Nz(Me.txtFilter.Text, "") = "" Then
        Me.FilterOn = False
        Me.txtFilter.SetFocus
        Exit Sub
    End If
    Me.Filter = "lastName like '*" + Me.txtFilter.Text + "*' or userName like '*" & _
        Me.txtFilter.Text + "*'"
    Me.FilterOn = True
    Me.txtFilter.SetFocus
    Me.txtFilter.Value = search_text
    Me.txtFilter.SelStart = Len(Nz(Me.txtFilter.Text, "")) + 1


End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文