SQL statement in VBA

skwilliams

Registered User.
Local time
Today, 07:36
Joined
Jan 18, 2002
Messages
516
I have an unbound form 'frmWeeklyReprints" with an unbound date field "Text150". I also have a query "qryOCParts1" with sum totals based on another query "qryOCParts" to get totals for each day. I'd like to select a date from Text150 and through VBA query qryOCParts1 to pull the matching date. Here's my SQL statement.

strSQL = "SELECT Sum(qryOCParts1.Count) AS SumOfCount " & vbCrLf & _
"FROM qryOCParts1 " & vbCrLf & _
"GROUP BY qryOCParts1.EndOfWeek " & vbCrLf & _
"HAVING (((qryOCParts1.EndOfWeek)=[Text150]));"

strSQL comes up null.

Any ideas??
 
The variable is Null or the result of using it is Null? For starters, you have to concatenate the form reference into the string, with appropriate delimiters. For a date that would look like:

"HAVING qryOCParts1.EndOfWeek = #" & [Text150] & "#"

By the way, you don't need vbCrLf in the code. Access will ignore them.
 
I forgot about the date delimiters.

When I debug and run the procedure, then mouse over strSQL it says
strSQL=""
 
Here's a sample of the data in qryOCParts1
SumOfCount EndOfWeek
2566 3/31/2012
6960 3/24/2012
7488 3/17/2012

If I selected 3/24/2012 from Text150 then Text16 prefills with 6960.

I'm getting this error.
Too few parameters. Expected 1.

The immediate window says:
Code:
SELECT Sum(qryOCParts1.Count) AS SumOfCount FROM qryOCParts1 GROUP BY qryOCParts1.EndOfWeek HAVING qryOCParts1.EndOfWeek = #3/24/2012#
 
Your description says the field name is "SumOfCount" but you have "Count" in the SQL. If it's really SumOfCount, that would cause that error.
 
When I run the code now, here's what I get.

Run-time error '3075':
Syntax error in date in query expression
'qryOCParts1.EndOfWeek=#[Text150]'.

Here's what shows up in the Immediate window.

Code:
SELECT Sum(qryOCParts1.Count) AS SumOfCount 
FROM qryOCParts 
GROUP BY qryOCParts1.EndOfWeek 
HAVING qryOCParts1.EndOfWeek=#[Text150]#;
 
What does your code look like? It looks like you aren't concatenating the form reference. You were in the last post.
 
I was using SumOfCount as the name for the counts sum. Its called count in qryOCParts but called SumOfCount in qryOCParts1 because I'm getting a daily total. Does that make sense or am I missing something? (Which is verrry possible)
 
You're querying qryOCParts1, so you have to use whatever fieldname qryOCParts1 returns; SumOfCount.
 
So use that name in your SQL.
 
I've just attached a stripped down version of the database with all relevant tables, queries, forms, and VBA.
 

Attachments

Users who are viewing this thread

Back
Top Bottom