0

So I am trying to figure out what I am doing wrong, I have a table I am trying to run a query which asks the user if they have a specific district they are looking for....

so my columns are

name    district   etc
Jay       1
Tom       3
Mary      5
Tim       5
Mike      15

I am trying to be able to have a parameter [which district?:] 1,5 .. It would display

Jay      1
Mary     5
Tim      5

Code:

WHERE 
    [District] Like "*" & [What District(s)] 
    & "*" OR [What District(s)] Like "*" & [District] & "*"

but I am getting

Jay       1
Mary      5
Tim       5
Mike      15

I am trying to avoid getting the "15" record.

2
  • Give this a try-- WHERE [District] In [What District(s)] Commented Sep 13, 2019 at 20:49
  • Won’t return any response when I put a , into the parameter. Commented Sep 13, 2019 at 22:09

2 Answers 2

1

Consider reversing the LIKE expressions and checking by comma positions for 1) comma after (#,) 2) comma before (,#), or 3) before and after (,#,) and then exact or no comma (=).

SELECT *
FROM Employees
WHERE 
       [What District(s)] LIKE [District] & ',*'
    OR [What District(s)] LIKE '*,' & [District]  & ',*'
    OR [What District(s)] LIKE '*,' & [District] 
    OR [Employee ID] = [What District(s)]

Advise user not to include whitespaces between entries as a complexity error strangely raises.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me ! I’m testing it to make sure no issues but looks good to go. Thanks
1

If you are dealing with a list of numbers separated by a quote, you can use the IN operator, like:

""WHERE [District] IN ("" & [What District(s)] & "")"" 

This would generate a valid SQL expression like:

WHERE [District] IN (1,5)

Reference : the IN operator in ms-access

3 Comments

This will not work if [What District(s)] is a parameter instead of concatenated variable.
@Parfait: probably not indeed. But since the OP is showing concatenated variables in the first place, this still looks like an acceptable solution...
Parameters can be concatenated inside string literals.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.