filter continuous form using textbox

北慕城南 提交于 2019-12-04 21:50:10
craig.white

I got it to work using this: .Filter = "LastName Like """ & filterval & """"

Need those annoying String Identifiers even for strings sometimes.

Okay, To get the form to open with no records and then pull up just the records you (or the user) specifies is easiest with a bit of re-work. (I'd recommend you working with a copy and not your original) 1:On your Continuous Form, remove the Recordsource; we're going to use Late Binding (Kinda) 2:Then delete the code under the txtFilter box, then delete the box itself. 3:Add a comboBox with something like this as the recordsource: SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName; (This will get you a unique list of last names so knowing how to spell the name will not be necessary, plus it assures at least one match) 4:In the After Update event of that combobox, add code like this:

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

Obviously you'll need to change the table and field names as necessary, but hopefully you get the idea.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!