【问题标题】:filter continuous form using textbox使用文本框过滤连续表单
【发布时间】:2013-11-26 14:05:13
【问题描述】:

我需要让用户使用用户在文本框中输入的值来过滤连续表单。并且连续表单也嵌套在几个级别的导航子表单中。这听起来很简单,但我在网上找到的所有示例都使用宏而不是 vba。

我设置了结构并为文本框txtFilter编写了一个更新后过程,如下所示:

Private Sub txtFilter_AfterUpdate()
    Dim filterval As String
    filterval = txtFilter.Value
    With Forms!Main!NavigationSubform.Form!NavigationSubform.Form
        .Filter = "LastName Like " & filterval
        .FilterOn = True
    End With
End Sub

我玩过不同的语法,但似乎都不能正常工作。这是从文件共享站点下载数据库相关部分的链接:http://jmp.sh/v/HGctZ4Ru74vDAjzN43Wq

谁能告诉我如何改变它以便用户可以使用文本框来过滤连续形式?

【问题讨论】:

    标签: ms-access vba ms-access-2010


    【解决方案1】:

    我使用它来工作:.Filter = "LastName Like """ & filterval & """"

    有时甚至需要那些烦人的字符串标识符。

    好的,要让表单在没有记录的情况下打开,然后只提取您(或用户)指定的记录,这是最简单的,只需进行一些返工。 (我建议您使用副本而不是您的原件) 1:在您的连续表格上,删除记录源;我们将使用后期绑定(有点) 2:然后删除txtFilter框下的代码,然后删除框本身。 3:添加一个像这样的组合框作为记录源: SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName;(这将为您提供一个唯一的姓氏列表,因此无需知道如何拼写该名称,而且它可以确保至少匹配一次) 4:在该组合框的更新后事件中,添加如下代码:

    Dim strSource As String
    strSource = "SELECT mt.IntakeNumber, mt.ClientNumber, " & _
        "mt.LastName, mt.FirstName, mt.ConsultationDate " & _
        " FROM myTable mt " & _
        "WHERE (mt.LastName)= '" & Me.cboFilter.Value & "'"
    
    Me.RecordSource = strSource
    Me.Requery
    

    显然,您需要根据需要更改表和字段名称,但希望您明白这一点。

    【讨论】:

    • 您可能还需要星号或星号:Filter = "LastName Like '*" & filterval & "*'"
    • HK1 的答案更好——这将允许任何匹配,而不仅仅是精确。
    • @craig.white +1 谢谢。现在我如何得到它,以便在加载表单时连续表单出现空?基础数据表中会有数万条记录,我只希望用户看到符合输入到 txtFilter 文本框中的条件的记录子集。目前,表格的全部内容都会在表单加载时显示在结果中。但是当用户在 txtFilter 中输入内容时,搜索过滤器会起作用
    • @craig.white 谢谢。我将把它标记为答案。但是,我只是使用 HK1 对您建议的编辑,因为我想保持代码简单,以便随着时间的推移更易于维护。
    • 不用担心,很高兴我们能提供帮助。
    【解决方案2】:

    Option Compare Database
    Option Explicit '<- always include this!!!!!
    
    Private Sub txtFilter_AfterUpdate()
        Dim strFilter As String
    
        ' only set Filter when text box contains something
        ' to search for ==> don't filter Null, empty string,
        ' or spaces
        If Len(Trim(Me.txtFilter.Value & vbNullString)) > 0 Then
            strFilter = "LastName Like '*" & _
                Replace(Me.txtFilter.Value, "'", "''") & _
                "*'"
            ' that Replace() prevents the procedure from breaking
            ' when the user enters a name with an apostrophe
            ' into the text box (O'Malley)
            Debug.Print strFilter ' see what we built, Ctrl+g
            Me.Filter = strFilter
            Me.FilterOn = True
        Else
            ' what should happen here?
            ' maybe just switch off the filter ...
            Me.FilterOn = False
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 2019-05-25
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多