We help IT Professionals succeed at work.

Access: Use comma separated textbox value as filter for query using WHERE (field IN (Forms!formname!textbox))

Last Modified: 2012-06-27
Hi -

I have a multiselect listbox that dumps its values out into a textbox in the form of a comma separated list.  The values look like this:


I want to use those values to filter a query, like this:

SELECT * FROM table WHERE (field IN (Forms!myform!textbox))

This is not working, though.  The query returns no results.  If I explicitly state the values in the query, it works as expected, like this:

SELECT * FROM table WHERE (field IN ('wv13Ce','Rja3SH','JifFZ1','VR0UHl'))

I've tried wrapping the form field and its values in double quotes, single quotes, no quotes, but it doesn't seem to matter.  If I'm referencing the value of the form field, the query doesn't work.

I can use VBA to rewrite the SQL of the query, but I'd like to avoid this if possible, as I have dozens of queries that need to do this (and I'm not sure how well this would work on a multi-user db).

Any ideas / suggestions?

Watch Question

Top Expert 2016
This one is on us!
(Get your first solution completely free - no credit card required)
Top Expert 2016

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Well, you could build the query from scratch in the click event of a button on your form, but you cannot refer to it in the query syntax the way that you have.  Try:

Private Sub cmd_BuildQuery

    Dim strSQL as string

    strSQL = "Select * FROM table WHERE [fieldname] IN (" & me.textbox & ")"

    'do something with that here, maybe update the SQL property of a saved query or change the RecordSource of a form or the RowSource of a control

End Sub
Top Expert 2016

and you can do this too,  this is what i use in most cases

Function InPar(sFld, Param)
Dim parArr, j
parArr = Split(Param, ",")
For j = 0 To UBound(parArr)
    If Trim(parArr(j)) = Trim(sFld) Then
        InPar = -1
        Exit Function
    End If
End Function

then use a query like this

select * from table
where InPar([FieldName],[Forms].[nameofForm].[textboxName])=True


This does it. I guess I'll never understand these odd Access-isms.  To state it more explicitly, the InStr() function is the key, and the working syntax of the SQL looks like this:

SELECT field
FROM table
WHERE (((InStr([Forms]![myform]![textbox],[field]))>0 Or (InStr([Forms]![myform]![textbox],[field])) Is Null));

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.