【问题标题】:Filter Subform, Starts With, Multiple criteria过滤子表单,开始于,多个条件
【发布时间】:2015-08-20 05:06:52
【问题描述】:

我想根据放入文本框中的数据过滤子表单。 如果Student ID 输入正确,下面的代码将显示记录。 我想让我的过滤器显示Student ID、First Name 或Last Name 以Me.Sort.Text 开头的任何记录

Private Sub Sort_Change()
If IsNull(Me.Sort.Text) Or Me.Sort.Text = "" Then
    Me.Students_subform.Form.FilterOn = False
Else
    Me.Students_subform.Form.FilterOn = True
    Me.Students_subform.Form.Filter = "[Student ID] ='" & Me.Sort.Text & "'"
End If
End Sub

【问题讨论】:

  • 那么如果Me.Sort.Text包含文本"foo",你想构建一个过滤表达式字符串比如"[Student ID] Like 'foo*' OR [First Name] Like 'foo*' OR [Last Name] Like 'foo*'"我理解对了吗?
  • 我认为这是正确的

标签: ms-access vba


【解决方案1】:

在我看来,您需要一个带有 3 个 Like 条件 OR 的过滤器表达式。

例如,如果Me.Sort.Text 包含文本“foo”,则构建过滤表达式字符串,例如...

 "[Student ID] Like 'foo*' OR [First Name] Like 'foo*' OR [Last Name] Like 'foo*'"

我在 Access 2010 中测试了这段代码,它会按照我认为你想要的方式过滤子表单...

Private Sub Sort_Change()
    Dim strFilter As String

    With Me.Sort
        If Len(.Text & vbNullString) > 0 Then
            strFilter = "[Student ID] Like '" & .Text & _
                "*' OR [First Name] Like '" & .Text & _
                "*' OR [Last Name] Like '" & .Text & "*'"
        End If
    End With
    Debug.Print "strFilter: " & strFilter '<- inspect this in Immediate window
                                          ' Ctrl+g will take you there
    With Me.Students_subform.Form
        If Len(strFilter) = 0 Then
            .FilterOn = False
        Else
            .Filter = strFilter
            .FilterOn = True
        End If
    End With
End Sub

【讨论】:

  • 很好,我以为这就是你所追求的。让我有点困扰的一点是,您将在 Sort 文本框中的每次击键时更改子表单 .Filter。如果这被证明对数据库引擎造成过多的负载,请考虑使用文本框的 After Update(而不是其 Change 事件)来应用 .Filter
  • 这是个好主意。如果我开始有问题,我会切换到那个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 2022-07-21
  • 2021-07-15
  • 2021-12-14
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多