View Single Post
  #2  
Old May 4th, 2010, 08:55 AM posted to microsoft.public.access.queries
Stefan Hoffmann
external usenet poster
 
Posts: 991
Default Query Inside an SQL statement

hi,

On 04.05.2010 09:21, Mr. Bud wrote:
Hi and TIA. What I'm wondering is can you use aquery name in place of a
table name when building an sql statement in code?

You can.

Example below can I
replace MyTable with MyQuery. It bombs out on me when I run the code.

hmm, "bombs out" is not a quite accurate error description.

Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
If Me!lstUIC_ID.ItemsSelected.Count 0 Then
For Each varItem In Me!lstUIC_ID.ItemsSelected
strCriteria = strCriteria & "MyTable.UIC = " & Chr(34) _
& Me!lstUIC_ID.ItemData(varItem) & Chr(34) & "OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
Else
strCriteria = "MyTable.UIC Like '*'"
End If
strSQL = "SELECT * FROM MyTable" & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL

Use Debug.Ptin strSQL to output the SQL statement to the immediate
window. Copy'n'paste it into a new query. Try to fix it. Then adjust
your code...




mfG
-- stefan --