Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL WHERE syntax question

Status
Not open for further replies.

VickyC

Technical User
Sep 25, 2010
206
CA
hi to all

My code opens a DAO recordset based on the following SQL....

strSQL = "SELECT * FROM " & strTbl & " WHERE " & strTbl & ".[Factor] IN(" & arrF(0) & ", " & arrF(1) & ..... & arrF(12) & ")"

This works perfectly, but the array values must all be listed individually in the 'IN' clause. Whenever I use this code, I can have more or less values in the array. Here's my question...

Is there a way to simply use something like...

strSQL = "SELECT * FROM " & strTbl & " WHERE " & strTbl & ".[Factor] IN(" & arrF & ")"

I can't seem to get a syntax that works in the 'IN' clause..

Thank you
Vicky
 

Would you tell us what you get in Immediate Window when you do that:
[codee]
strSQL = "SELECT * FROM " & strTbl & " WHERE " & strTbl & ".[Factor] IN(" & arrF(0) & ", " & arrF(1) & ..... & arrF(12) & ")"
[blue]
Debug.Print strSQL
Debug.Print "arrF(0) is " & arrF(0)
Debug.Print "arrF(1) is " & arrF(1)
Debug.Print "arrF(12) is " & arrF(12)[/blue]
[/code]
What is [tt][Factor][/tt] defined as in your data base? String? Number? Date?

Have fun.

---- Andy
 
I concur with Duane.

This is untested but maybe something like:

Code:
inclause= 0
Items = UBound(arrF)
For x = 0 To Items
if len(arrf(x))>0 then 
inclause= inclause & arrf(x) & ","
end if
next
If Right(inclause, 1) = "," Then
inclause = Left(inclause, Len(inclause)-1)
End If
...
...
strSQL = "SELECT * FROM " & strTbl & " WHERE " & strTbl & ".[Factor] IN(" & inclause & ")"

Assumes a numeric data type which as Andy points out is a problem if it it text data or dates or something.
 
Why not simply this ?
Code:
strSQL = "SELECT * FROM " & strTbl & " WHERE Factor IN(" & Join(arrF, ",") & ")"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I should have mentioned that all values of arrF() are integers. Sorry.
 
Why not.."

Didn't even know JOIN existed. Wow. I love diplaying my ignorance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top