markm said:
This will seem trivial to most others, but I'm having
trouble building a SQL query that uses text qualifiers.
What I'm TRYING to do is to count the number of records in
a Dynaset that is created from a Query and return it in a
variable. I'm having problems with the query part. It
works fine is a Query, but obviously not in VB. I know
that the proble is with the quotes, but I can't find any
rules in the literature that I have available to me.
Could someone explain the rules and help with the SQL
statement and count? Thanks and Happy Holidays!
My code follows:
xQueryCriteria = "SELECT * FROM TASKsubm WHERE
(((TASKsubm.TASK_STAT) Is Null Or (TASKsubm.TASK_STAT)
= "In-Progress" & Or (TASKsubm.TASK_STAT) = "Hold")
Set MyRS = MyDB.OpenRecordset(xQueryCriteria)
MyRS.MoveLast
MyRightRecordCount = MyRS.RecordCount
From the looks of things your problem is in the embedding of the
double-quote character (") inside a string literal that is itself
delimited by double-quotes. You started with SQL from the query
designer, which I'm going to reformat slightly for clarity:
SELECT * FROM TASKsubm WHERE
(TASKsubm.TASK_STAT Is Null)
Or (TASKsubm.TASK_STAT = "In-Progress")
Or (TASKsubm.TASK_STAT = "Hold")
I've adjusted the parentheses, removing unnecessary ones added by the
query designer but leaving a pair around each subclause of the WHERE
clause.
Now you need to transform this whole thing into one quoted string and
assign it to xQueryCriteria. You could make it into one long string,
but I find it's best to break the string into parts, one on each line,
and concatenate them together -- it's easier to read that way. Taking
that approach, the next incarnation of the statement would be:
'*** NOTE: THE FOLLOWING DOESN'T WORK
xQueryCriteria = _
"SELECT * FROM TASKsubm WHERE " & _
"(TASKsubm.TASK_STAT Is Null) " & _
"Or (TASKsubm.TASK_STAT = "In-Progress") " & _
"Or (TASKsubm.TASK_STAT = "Hold")"
So far, so good, but we've still got a problem caused by the embedded
double-quotes around "In-Progress" and "Hold". They will cause
premature termination of the quoted literal, and confuse the dickens out
of the VBA compiler.
Now, in this case, since you know the text values that must be embedded
in the SQL string, and you know that they won't contain the single-quote
character ('), you can replace the embedded double-quotes with
single-quotes:
xQueryCriteria = _
"SELECT * FROM TASKsubm WHERE " & _
"(TASKsubm.TASK_STAT Is Null) " & _
"Or (TASKsubm.TASK_STAT = 'In-Progress') " & _
"Or (TASKsubm.TASK_STAT = 'Hold')"
That works, because the single- and double-quote characters may both be
used to delimit a text value in SQL (though not in VBA).
A more general solution, when the literal values may contain the
single-quote character, is to "double-up" the double-quote characters
inside the string literal, like this:
xQueryCriteria = _
"SELECT * FROM TASKsubm WHERE " & _
"(TASKsubm.TASK_STAT Is Null) " & _
"Or (TASKsubm.TASK_STAT = ""In-Progress"") " & _
"Or (TASKsubm.TASK_STAT = ""Hold"")"
That works because inside a quoted string literal, a single occurrence
of the quote character may be represented by a pair of that character
placed side by side. For example, the string literal
"ABC""DEF"
evaluates to
ABC"DEF
Another subterfuge often used is to use the function Chr(34) to return
the double-quote character. So you could also write this:
xQueryCriteria = _
"SELECT * FROM TASKsubm WHERE " & _
"(TASKsubm.TASK_STAT Is Null) " & _
"Or (TASKsubm.TASK_STAT = " & _
Chr(34) & "In-Progress" & Chr(34) & ") " & _
"Or (TASKsubm.TASK_STAT = "
Chr(34) & "Hold" & Chr(34) & ")"
That will work, too, but seems a bit obscure for your current purpose.