Search code examples
ms-accessdynamic-queries

Set up a parameter with multiple criteria in a query?


I am trying to set up a parameter in a query which will ask the user for two different letters, and will then display all records that have info which starts with either of those letters typed in by the user. What code would I put in the criteria part to accomplish this? Thanks


Solution

  • Like "[" & [Enter 2 letters] & "]*"
    

    The user would enter, for example, ad or da. They could enter more than 2 letters.

    If you want specifically 2 letters, or just more control, then you'll need to use VBA, and perhaps a TextBox on a Form, rather than a simple parameter-query.

    As you want two dialogs (parameter boxes) you can use:

    Like [First letter] & "*" Or Like [Second letter] & "*"
    

    Again, they can enter more than a single letter in each box - which I consider a useful feature. You could restrict it to a single letter each with:

    Like Left([First letter],1) & "*" Or Like Left([Second letter],1) & "*"
    

    If they don't enter anything into the boxes then it will show all records. As mentioned, VBA would be needed to control the criteria more precisely.

    If you really wanted to restrict to a single letter each then you can use:

    Like IIf(Len([First letter])=1,[First letter] & "*",False) Or Like IIf(Len([Second letter])=1,[Second letter] & "*",False)