G
Guest
Sorry to keep bothering you guys but I really hope you can help me out yet
again before my hair has completely fallen out.
I've got over 20 queries that I want vb code to 'build' via SQL. The
queries are very similar in that they are counting the number of records
within a particular field. I'm trying to get the code to loop through each
query and to refer to each (different) table field name by storing it in a
variable.
Here's the code:
st1 = "name of table field 1"
st2 = "name of table field 2"
st3 = "name of table field 3"
st4 = "name of table field 4" ...and so on up to st22
intQueryRef = 1
intStrRef = 1
Do Until intQueryRef = 23
stDocName = "qry_Stats" & intQueryRef
Set loqd = CurrentDb.QueryDefs(stDocName)
stSQL = "SELECT Count(tbl_Reports.Number) AS CountOfNumber "
stSQL = stSQL & "FROM tbl_Reports LEFT JOIN qry_Entries ON
tbl_Reports.Number = qry_Entries.Number "
stSQL = stSQL & "WHERE tbl_Reports." & " & st" & intStrRef & " = True;"
loqd.SQL = stSQL
intQueryRef = intQueryRef + 1
intStrRef = intStrRef + 1
Loop
However, I can't seem to get the WHERE clause to refer to the value stored
in st1 or st2 (etc..). Instead the WHERE clause reads literally "WHERE
tbl_Reports. & st1 = True"
How can I get the VALUE within the variable to replace the " & st1" bit of
the clause.
ALSO, while I've got your attention (!!) -
In some of the queries I'll be inserting an "IN" statement. Is it OK to
include a WHERE IN phrase in cases where there is already a WHERE clause
preceding it? Or should I just put "AND IN" instead?
Sorry for the waffle.
Any help would be very much appreciated.
Kind regards,
Lee
again before my hair has completely fallen out.
I've got over 20 queries that I want vb code to 'build' via SQL. The
queries are very similar in that they are counting the number of records
within a particular field. I'm trying to get the code to loop through each
query and to refer to each (different) table field name by storing it in a
variable.
Here's the code:
st1 = "name of table field 1"
st2 = "name of table field 2"
st3 = "name of table field 3"
st4 = "name of table field 4" ...and so on up to st22
intQueryRef = 1
intStrRef = 1
Do Until intQueryRef = 23
stDocName = "qry_Stats" & intQueryRef
Set loqd = CurrentDb.QueryDefs(stDocName)
stSQL = "SELECT Count(tbl_Reports.Number) AS CountOfNumber "
stSQL = stSQL & "FROM tbl_Reports LEFT JOIN qry_Entries ON
tbl_Reports.Number = qry_Entries.Number "
stSQL = stSQL & "WHERE tbl_Reports." & " & st" & intStrRef & " = True;"
loqd.SQL = stSQL
intQueryRef = intQueryRef + 1
intStrRef = intStrRef + 1
Loop
However, I can't seem to get the WHERE clause to refer to the value stored
in st1 or st2 (etc..). Instead the WHERE clause reads literally "WHERE
tbl_Reports. & st1 = True"
How can I get the VALUE within the variable to replace the " & st1" bit of
the clause.
ALSO, while I've got your attention (!!) -
In some of the queries I'll be inserting an "IN" statement. Is it OK to
include a WHERE IN phrase in cases where there is already a WHERE clause
preceding it? Or should I just put "AND IN" instead?
Sorry for the waffle.
Any help would be very much appreciated.
Kind regards,
Lee