Multiple search Problem (1 Viewer)

ECEK

Registered User.
Local time
Today, 04:49
Joined
Dec 19, 2012
Messages
717
This is my filter code

Code:
Private Sub subChangeFilter()
If Trim(Me.textBox1 & Me.textBox2 & Me.textBox3 & Me.textBox4 & Me.textBox5 & "") = "" Then
Me.Filter = False
Else
Me.Filter = "surcomp LIKE '*" & Replace(Me.textBox1, "'", "''") & "*' _
AND First_Name LIKE '" & Me.textBox2 & "*' _
AND Address_Line_1 LIKE '*" & Me.textBox3 & "*' _
AND PostCode LIKE '*" & Me.textBox4 & "*' _
AND Home_Telephone_Number LIKE '*" & Me.textBox5 & "*'"

Me.FilterOn = True
End If

End Sub

I have managed to sort out the ' problem (as in searching for O'Neil etc) however:

EG
When I search "Smith" in surcomp (from textBox1) my filter (results) gives me all of the "Smiths" but only the ones where ALL of my other filter fields are populated.

If I filter on just one textBox then only THAT textBox should apply.

Hope you understand, Hope you can help?
 

moke123

AWF VIP
Local time
Yesterday, 23:49
Joined
Jan 11, 2013
Messages
3,920
try testing each control for a value and build up your filter string.

Code:
if nz(me.textbox1,"")<>"" then
strFilter = strFilter &  "surcomp like ""*" & me.textbox1 & "*"" and  "
end if

After doing this for each control you remove the trailing "And"

Code:
if strfilter <> "" then
strFilter = left(strFilter,len(strFilter)-4)
end if

Debug.print strfilter

then apply the filter
Code:
me.filter = strFilter
me.filteron = true
 

ECEK

Registered User.
Local time
Today, 04:49
Joined
Dec 19, 2012
Messages
717
Hi Moke
Thanks for the response.
I'm afraid Im a bit lost as to where to put your suggestion within my existing code.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Yesterday, 23:49
Joined
Oct 17, 2012
Messages
3,276
Basically, you need to check each field they can possibly filter on. If they've entered something, you add that field to a where clause, and if they didn't, you skip it.

Here's an example from an app I inherited and am in the midst of cleaning up. (I used the Tag approach because there are 11 different things they can search on.)

Code:
Private Sub btnSearch_Click()
 
'Update and run pass-through query

On Error GoTo Err_btnSearch_Click
 
Dim DB As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As QueryDef
Dim WhereClause As String
 
    WhereClause = BuildWhere()

    If WhereClause = "" Then
        Beep
        MsgBox "Please enter at least one item to search by!", , PROJECT_NAME
    Else
        Set DB = CurrentDb()
        Set qdf = DB.QueryDefs("SearchFormQuery")
        
        qdf.SQL = SQL_START + WhereClause
            
        Set qdf = Nothing
        
        Set rs = DB.OpenRecordset("SearchFormQuery")
        If rs.RecordCount = 0 Then
            MsgBox "No records match this criteria"
        Else
            DoCmd.OpenQuery "SearchFormQuery"
        End If
    End If
 
Exit_btnSearch_Click:
    On Error Resume Next
    If Not rs Is Nothing Then
        rs.Close
        Set rs = Nothing
    End If
    If Not qdf Is Nothing Then Set qdf = Nothing
    If Not DB Is Nothing Then Set DB = Nothing
    Exit Sub
 
Err_btnSearch_Click:
    MsgBox Err.Description
    Resume Exit_btnSearch_Click
    
End Sub
 
 
Private Function BuildWhere() As String
 
On Error GoTo BW_Err
 
Dim ctrl As Control
Dim WhereClause As String
 
    WhereClause = "WHERE "
    
    For Each ctrl In Me.Controls
        Select Case ctrl.Tag
            Case "Full"
                If Nz(ctrl.Value, "") <> "" Then WhereClause = WhereClause & ctrl.Name & " = '" & ctrl.Value & "' And "
            Case "Like"
                If Nz(ctrl.Value, "") <> "" Then WhereClause = WhereClause & ctrl.Name & " Like '%" & ctrl.Value & "%' And "
        End Select
    Next ctrl
    
    If Not IsNull(Me!AddedDateStart) Then
        If IsNull(Me!AddedDateEnd) Then
            WhereClause = WhereClause & "AddedDate = '" & Me!AddedDateStart & "'"
        Else
            WhereClause = WhereClause & "AddedDate Between '" & Me!AddedDateStart & "' And '" & Me!AddedDateEnd & "'"
        End If
    End If
    
    If Right(WhereClause, 5) = " And " Then WhereClause = Left(WhereClause, Len(WhereClause) - 5)
    If WhereClause = "WHERE " Then WhereClause = ""
    
    BuildWhere = WhereClause
    
BW_Exit:
    Exit Function
 
BW_Err:
    Beep
    MsgBox ERR_START & _
           "In Function:" & vbTab & "SearchQuery.BuildWhere" & vbCrLf & _
           "Err Number: " & vbTab & Err.Number & vbCrLf & _
           "Description: " & vbTab & Err.Description, vbCritical, PROJECT_NAME
    Resume BW_Exit
    
End Function
SLQ_START is a constant set at the module level that just has the SELECT...FROM portion of the
 

ECEK

Registered User.
Local time
Today, 04:49
Joined
Dec 19, 2012
Messages
717
This what I did but it errors that Runtime 3075 Syntax Error:

Private Sub subChangeFilter()

If Nz(Me.t1, "") <> "" Then
strfilter = strfilter & "surcomp like ""*" & Me.t1 & "*"" and "
End If

If Nz(Me.t2, "") <> "" Then
strfilter = strfilter & "First_Name like ""*" & Me.t2 & "*"" and "
End If

If Nz(Me.t3, "") <> "" Then
strfilter = strfilter & "Address_Line_1 like ""*" & Me.t3 & "*"" and "
End If
If Nz(Me.t4, "") <> "" Then
strfilter = strfilter & "PostCode like ""*" & Me.t4 & "*"" and "
End If

If Nz(Me.t5, "") <> "" Then
strfilter = strfilter & "Home_Telephone_Number like ""*" & Me.t5 & "*"" and "
End If

If strfilter <> "" Then
strfilter = Left(strfilter, Len(strfilter) - 4)
End If

Debug.Print strfilter

Me.Filter = strfilter
Me.FilterOn = True

End Sub
 

JHB

Have been here a while
Local time
Today, 05:49
Joined
Jun 17, 2012
Messages
7,732
This what I did but it errors that Runtime 3075 Syntax Error:
Try by replacing the " marked with red to ' in all code lines.
Code:
From
... like [B][COLOR=Red]""[/COLOR][/B]*" & Me.t1 & "*[B][COLOR=Red]"" [/COLOR][/B]and  "
To
...like [COLOR=Red][B]'[/B][/COLOR]*" & Me.t1 & "*[B][COLOR=Red]' [/COLOR][/B]and  "
 

ECEK

Registered User.
Local time
Today, 04:49
Joined
Dec 19, 2012
Messages
717
JHB This has revolutionised my form !!
Thank you so much.
This works a treat.
 

JHB

Have been here a while
Local time
Today, 05:49
Joined
Jun 17, 2012
Messages
7,732
You're welcome, good luck.
 

Users who are viewing this thread

Top Bottom