Access Vba - Apply Filter - Multiple or Conditions
I Converted My Working Macro to Vba Script (Also Working) in Microsoft Access 2010: Docmd. Applyfilter "", "[Forename] Like ""*"" & [Forms]! [Stafftotalquery]!...
I converted my working macro to VBA script (also working) in Microsoft Access 2010:
DoCmd.ApplyFilter "", "[Forename] Like ""*"" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & ""*""", ""
I used the same formatting (I believe) to try and extend this filter to work with multiple fields, though it doesn't work:
DoCmd.ApplyFilter "", "[Forename] Like ""*"" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & ""*"" Or [Surname] Like ""*"" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & ""*""", ""
This is what I mean by doesn't work: If my data is: [forename] [surname] alex bobs chris dean
then typing, for example "alex", or "a", doesn't filter the results at all. On the other hand, the code with just one filter does narrow down the data.
1 Answer
You could try an alternative method of applying a filter:
me.filter = "[forename] like '*" & Me.StaffTotalSearchText & "*'" & _
" OR [surname] like '*" & Me.StaffTotalSearchText & "*'"
me.filter =true
EDIT
As @Andre has commented, I have used single quotes (apostrophes) to encapsulate my strings. Your vba should work with the following change:
DoCmd.ApplyFilter "", "[Forename] Like '*" & _
[Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'" & _
" Or [Surname] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText] & "*'"