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]![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.

2

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] & "*'"
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest