noahisaac
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:
'wv13Ce','Rja3SH','JifFZ1' ,'VR0UHl'
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?
Thanks,
Noah
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:
'wv13Ce','Rja3SH','JifFZ1'
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
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?
Thanks,
Noah
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
Next
End Function
then use a query like this
select * from table
where InPar([FieldName],[Forms]. [nameofFor m].[textbo xName])=Tr ue
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
Next
End Function
then use a query like this
select * from table
where InPar([FieldName],[Forms].
ASKER
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]![t extbox],[f ield])) Is Null));
SELECT field
FROM table
WHERE (((InStr([Forms]![myform]!
https://www.experts-exchange.com/questions/22613609/Entering-Parameters-In-AN-ACCESS-Query.html?&anchorAnswerId=19217156#a19217156