Avatar of noahisaac
Flag for United States of America asked on

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

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?

Microsoft AccessSQL

Avatar of undefined
Last Comment

8/22/2022 - Mon
Rey Obrero (Capricorn1)

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rey Obrero (Capricorn1)

Dale Fye

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
Rey Obrero (Capricorn1)

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

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));