Form with multi-criteria searches, uses strings and filters

jaitken0308

Registered User.
Local time
Yesterday, 23:42
Joined
Jan 23, 2014
Messages
30
ANSWER: The code you seek (if you had the same question I did, anyway) can be found in post #11.


Not sure if this is the right place for this question, so please forgive me if it is. It does involve VBA, but also involves forms and searches. Here goes...

I have a search form with blank fields tied to a table, four criteria search boxes, and a button to take the input from the search boxes, search the table, and populate the results on the form's blank fields.
As of now, it works as long as all four criteria boxes aren't null.
I used filters to achieve this, and here's the code that works as long as all four boxes are not empty. (My criteria boxes are as follows: a textbox called "Keyword" and three combo boxes called HRCombo, BuildingCombo, and RoomCombo, and the fields they're tied to are as follows: "Item Description" "HR Holder" "Building" "Room") My first line "Me.Filter = ..." was broken up to make it easier to view.

Code:
Me.Filter = "[Item Description] Like " & Chr(34) & Me.Keyword & "*" & Chr(34) & "
AND [HR Holder] = '" & Me.HRCombo & "'" & " AND [Building] = '" & Me.BuildingCombo
& "'" & " AND [Room] = '" & Me.RoomCombo & "'"
Me.FilterOn = True
Me.Requery

I need it to be able to do the search no matter which combination of criteria boxes have input. Someone recommended using if statements to do the following: Create four strings, one for each criteria box. Use 4 if statements to check if the box is null - if it is null, assign an asterisk to its string, and if its not null, assign the value I used for the above Me.Filter statement to each box's string. Then, use Me.Filter and concatenate the four strings at the end.
Here's the code I used for this, and, with my limited knowledge, I can't get it to work.

Code:
Dim StrA as String, StrB as String, StrC as String, StrD as String
 
If Me.Keyword is null then
StrA = "*"
else
StrA = [Item Description] Like " & Chr(34) & Me.Keyword & "*" & Chr(34)
End If
 
If Me.HRCombo is null then
StrB = "*"
else
StrB = [HR Holder] = '" & Me.HRCombo & "'"
End If
 
If Me.BuildingCombo is null then
StrC = "*"
else
StrC = [Building] = '" & Me.BuildingCombo & "'"
End If
 
If Me.RoomCombo is null then
StrD = "*"
else
StrD = [Room] = '" & Me.RoomCombo & "'"
End If
 
Me.Filter = "StrA & " AND "StrB & " AND "StrC &" AND "StrD"
Me.FilterOn = True
Me.Requery

Like I said, I have a limited knowledge, so I'm sure there's probably missing quotes and commas, or too many of them. Any ideas? Thanks in advance for any help.
 
Last edited:
Try this out. I dont have MS Access handy so I couldn't test it.


Code:
Dim StrA as String, StrB as String, StrC as String, StrD as String

StrA = ""
StrB = ""
StrC = ""
StrD = ""

If Not isNull(Me.Keyword) then
	StrA = "[Item Description] Like '" & Me.Keyword & "'"
End If
 
If Not isNull(Me.HRCombo)  then
	StrB = "[HR Holder] = '" & Me.HRCombo & "'"
End If
 
If Not isNull(Me.BuildingCombo) then
	StrC = "Building = '" & Me.BuildingCombo & "'"
End If
 
If Not isNull(Me.RoomCombo) then
	StrD = "Room = '" & Me.RoomCombo & "'"
End If
 
Me.Filter = "StrA & " AND "StrB & " AND "StrC &" AND "StrD"
Me.FilterOn = True
Me.Requery
 
Also it sounds like you're expecting to return multiple records from the queries? If so you're probably better off using a report instead of a form.
 
Code:
Me.Filter = StrA & " AND " &  StrB  & " AND " & StrC & " AND " & StrD

Sorry your filter line was wrong too, replace with this
 
Also it sounds like you're expecting to return multiple records from the queries? If so you're probably better off using a report instead of a form.

Yes, it can return a single record or multiple records. The only reason I used a form is for ease of use for the user to edit records when they populate. The plan was to add in a report feature also.
 
Try this out. I dont have MS Access handy so I couldn't test it.


Code:
Dim StrA as String, StrB as String, StrC as String, StrD as String
 
StrA = ""
StrB = ""
StrC = ""
StrD = ""
 
If Not isNull(Me.Keyword) then
    StrA = "[Item Description] Like '" & Me.Keyword & "'"
End If
 
If Not isNull(Me.HRCombo)  then
    StrB = "[HR Holder] = '" & Me.HRCombo & "'"
End If
 
If Not isNull(Me.BuildingCombo) then
    StrC = "Building = '" & Me.BuildingCombo & "'"
End If
 
If Not isNull(Me.RoomCombo) then
    StrD = "Room = '" & Me.RoomCombo & "'"
End If
 
Me.Filter = "StrA & " AND "StrB & " AND "StrC &" AND "StrD"
Me.FilterOn = True
Me.Requery

I've got some code from somewhere else that now works... I'll try this as it seems simpler.

Another question, though. I originally used an asterisk so it would take the user-typed keyword and find results in the table, but I only had it at the end of the statement (see my original code below)
Code:
StrA = [Item Description] Like " & Chr(34) & Me.Keyword & "*" & Chr(34)
I want to add it to the beginning, because the way it is now, it will only find a record that starts with the keyword, and it was intended to find any record with the keyword in it. How would I add it in? I can't find a way to do it correctly... pesky single and double quotes are getting the best of me :)
 
Does my code work? I have been unable to test it so am writing from memory lol.

My mistake with the LIKE statement. Try this out

Code:
StrA = "[Item Description] Like '" & "%" & Me.keyword & "%'"

That should return Item Description that contains the keyword.
 
Does my code work? I have been unable to test it so am writing from memory lol.

My mistake with the LIKE statement. Try this out

Code:
StrA = "[Item Description] Like '" & "%" & Me.keyword & "%'"

That should return Item Description that contains the keyword.


When I run it, it gives me this error:
Code:
Run-time error '3075':
 
Syntax error (missing operator) in query expression '[Item Description] Like "%mykeyword%' AND AND AND'
 
This work?

Code:
Dim queryString As String
 
    queryString = ""
     
    If Not IsNull(Me.keyword) Then
        queryString = queryString & "[Item Description] Like '" & "*" & Me.keyword & "*'" & " AND "
    End If
     
    If Not IsNull(Me.HRCombo) Then
        queryString = queryString & "[HR Holder] = '" & Me.HRCombo & "'" & " AND "
    End If
     
    If Not IsNull(Me.BuildingCombo) Then
        queryString = queryString & "Building = '" & Me.BuildingCombo & "'" & " AND "
    End If
     
    If Not IsNull(Me.RoomCombo) Then
        queryString = queryString & "Room = '" & Me.RoomCombo & "'" & " AND "
    End If
     
    If Len(queryString) > 1 Then
        queryString = RTrim(queryString)
        queryString = Left(queryString, Len(queryString) - 3)
    End If
    
    Me.Filter = queryString
    Me.FilterOn = True
    Me.Requery
 
Last edited:
This work?

Code:
Dim queryString As String
 
    queryString = ""
 
    If Not IsNull(Me.keyword) Then
        queryString = queryString & "[Item Description] Like '" & "%" & Me.keyword & "%'"
    End If
 
    If Not IsNull(Me.HRCombo) Then
        queryString = queryString & "[HR Holder] = '" & Me.HRCombo & "'" & " AND "
    End If
 
    If Not IsNull(Me.BuildingCombo) Then
        queryString = queryString & "Building = '" & Me.BuildingCombo & "'" & " AND "
    End If
 
    If Not IsNull(Me.RoomCombo) Then
        queryString = queryString & "Room = '" & Me.RoomCombo & "'" & " AND "
    End If
 
    If Len(queryString) > 1 Then
        queryString = RTrim(queryString)
        queryString = Left(queryString, Len(queryString) - 3)
    End If
 
    Me.Filter = queryString
    Me.FilterOn = True
    Me.Requery

Nope. :)

Run-time error '3705'

Syntax error in string in query expression '[Item Description] Like '%keyword%'

And it shortened my keyword by removing the last letter from it...
 
Have a try with this ...
Code:
Dim strSQL as String

  If Len(Me.Keyword & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Item Description] Like '*" & Me.Keyword & "*')"
  End If

  If Len(Me.HRCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([HR Holder] = '" & Me.HRCombo & "')"
  End If

  If Len(Me.BuildingCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Building] = '" & Me.BuildingCombo & "')"
  End If

  If Len(Me.RoomCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Room] = '" & Me.RoomCombo & "')"
  End If

  Me.Filter = strSQL
  Me.FilterOn = True
  Me.Requery

* is the wildcard for Access LIKE statements
% is the wildcard for Oracle LIKE statements, MySQL as well I think.
 
Nope. :)



And it shortened my keyword by removing the last letter from it...

Yea I missed something out. I have made changes to that post so it might work now. Really hard to test when not having a compiler handy :)
 
Have a try with this ...
Code:
Dim strSQL as String
 
  If Len(Me.Keyword & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Item Description] Like '*" & Me.Keyword & "*')"
  End If
 
  If Len(Me.HRCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([HR Holder] = '" & Me.HRCombo & "')"
  End If
 
  If Len(Me.BuildingCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Building] = '" & Me.BuildingCombo & "')"
  End If
 
  If Len(Me.RoomCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Room] = '" & Me.RoomCombo & "')"
  End If
 
  Me.Filter = strSQL
  Me.FilterOn = True
  Me.Requery

* is the wildcard for Access LIKE statements
% is the wildcard for Oracle LIKE statements, MySQL as well I think.

That did it. thanks for helping!
 
Yea I missed something out. I have made changes to that post so it might work now. Really hard to test when not having a compiler handy :)

I'm just glad you decided to help even though you couldn't test.
 
You're welcome. :)

No compiler handy either, just plenty of experience of building SQL statements by hand. ;)


One more question...

The code below can easily be altered to use with combo boxes and text boxes. How would you alter it for a checkbox?

Code:
If Len(Me.RoomCombo & vbNullString) > 0 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([Room] = '" & Me.RoomCombo & "')"
  End If
 
Ah, that's possibly more difficult as a checkbox can either be True (checked) or False (unchecked), unless its in an initial NULL state.

Off the top of my head I would say that you could do something like this.

Replace the checkbox with a combo box set up as follows:-
Column Count: 2
Row Source Type: value list
Row Source: -99, "N/A", True, "True", False, "False"
Column widths: 0
Default value: -99

That will give a list with 3 possible values:
"N/A" (or whatever you want) [Default] - excluded from SQL string
"True" - Field is True (checked)
"False" - Field is False (not checked)

I would then change the code to read ...

Code:
If nz(Me.cboCriteria.Column(0), -99) <> -99 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([myCriteria] = " & Me.cboCriteria.Column(0) & ")"
  End If

If the value "N/A" is picked it will return the value a value -99 so the SQL line will not be added.

If the value "True" is picked it will return a value of True so the SQL line will be added looking for True (checked) records.

If the value "False" is picked it will return a value of False so the SQL line will be added looking for False (unchecked) records.
 
Ah, that's possibly more difficult as a checkbox can either be True (checked) or False (unchecked), unless its in an initial NULL state.

Off the top of my head I would say that you could do something like this.

Replace the checkbox with a combo box set up as follows:-
Column Count: 2
Row Source Type: value list
Row Source: -99, "N/A", True, "True", False, "False"
Column widths: 0
Default value: -99

That will give a list with 3 possible values:
"N/A" (or whatever you want) [Default] - excluded from SQL string
"True" - Field is True (checked)
"False" - Field is False (not checked)

I would then change the code to read ...

Code:
If nz(Me.cboCriteria.Column(0), -99) <> -99 Then
    If Len(strSql & vbNullString) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "([myCriteria] = " & Me.cboCriteria.Column(0) & ")"
  End If

If the value "N/A" is picked it will return the value a value -99 so the SQL line will not be added.

If the value "True" is picked it will return a value of True so the SQL line will be added looking for True (checked) records.

If the value "False" is picked it will return a value of False so the SQL line will be added looking for False (unchecked) records.


Yep, that works. Question, though...

If other combo boxes don't necessarily need the "default" value put in for the string to exclude it, then why does this one? Couldn't I just remove the part about the "N/A" and the "99" and get the same result? The checkbox would have just asked the user to select yes or no, and if they didn't select anything, just as they didn't select anything in the other combo/text boxes, then the string would just have excluded it, right?
 
Yep, that works. Question, though...

If other combo boxes don't necessarily need the "default" value put in for the string to exclude it, then why does this one? Couldn't I just remove the part about the "N/A" and the "99" and get the same result? The checkbox would have just asked the user to select yes or no, and if they didn't select anything, just as they didn't select anything in the other combo/text boxes, then the string would just have excluded it, right?


Nevermind, I missed the part about the combo box being tied to a checkbox field in the table. You'd think since it was my DB that I would have noticed that before asking...:banghead:
 
No. No.

I would imagine that none of your criteria for selection were bound to any fields in any table. :confused: If they were you would be changing them every time someone changed the criteria.

You can use fields as "unbound" just by not setting a control source for them. They will still function in the same way but won't store any data.


The reason for using a combobox, instead of the checkbox, is that a checkbox can either be True or False. So you will find records which are either True OR False.

But what happens if you don't want to include it in the criteria at all?

The combo box with the "N/A" allows for the user to choose not select based on the value in the checkbox.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom