How to create a VBA code to search multiple fields in Access with one input box?

Anonymous
2025-03-28T20:43:38+00:00

Hello all,

I am having trouble making my search box work with all the fields. The PPNumYr field was originally a string and the numbers had leading zeros such 0197, 0297 and then I changed it to Numbers so the leading zero is now gone but there are numbers with zeros in the middle. I have posted my code below and I realize since I changed the field to numbers InStr does not work anymore but it did not work when the numbers were still a string. How can I fix this? Another field that I want to be searched is names of locations and I am not sure how to do that either, which is the second part of the code. Please help

If InStr(SearchText, "###") <> 0 Then

    DoCmd.OpenForm "PhotopointF", , , "PPNumYr="""  &   SearchText & """"

    Exit Sub

End If

If InStr(SearchText, "Out House", "East caves", "Bottom Hill") <> 0 Then

    DoCmd.OpenForm "PhotopointF", , , "LocationName="""  & SearchText & """"

    Exit Sub

End If
Microsoft 365 and Office | Access | Other | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Anonymous
    2025-03-28T21:51:46+00:00

    Thank you. I am having trouble with the row source property it is giving me an error

    0 comments No comments
  2. ScottGem 68,755 Reputation points Volunteer Moderator
    2025-03-28T23:25:42+00:00

    If you get an error message please provide the EXACT error message.

    it might also help us help you if we know more about the application and the data structure.

    0 comments No comments