TRY
rstTimeCardHoursMTD.Open "SELECT T_TimeCardHours.CaseNum, " & _
"Sum(Hours) AS TotCaseHrsMTD " & _
" FROM T_TimeCardHours INNER JOIN T_TimeCards " & _
" ON T_TimeCardHours.TimeCardID = T_TimeCards.TimeCardID " & _
" WHERE T_TimeCards.EmployeeID = " & lngAccessID & _
" AND DatePart("m", DateWorked) = " & lngMonth & _
" GROUP BY CaseNum"
I did miss that the HAVING clause might have a problem and it would be more
efficient to put the filter criteria into a where clause anyway.
I'm not sure you can use the apostrophes in place of the two quote marks.
DatePart is a VBA function and I know that an apostrophe won't work in VBA
module, but from SQL the apostrophe might get translated correctly.
By the way a good way to troubleshoot this would be to assign the SQL string
to a string variable. When you get an error you can DEbug.print the SQL
string to the immediate window and copy that to a blank query. The error
messages are usually better and often you will get the cursor placed at or
near the actual code causing the problem.
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
John,
I modified the code as follows:
rstTimeCardHoursMTD.Open "SELECT T_TimeCardHours.CaseNum, " & _
"Sum(Hours) AS TotCaseHrsMTD " & _
"FROM T_TimeCardHours INNER JOIN T_TimeCards " & _
"ON T_TimeCardHours.TimeCardID = T_TimeCards.TimeCardID " & _
" HAVING T_TimeCards.EmployeeID = " & lngAccessID & _
" AND DatePart('m', DateWorked) = " & lngMonth & _
" GROUP BY CaseNum"
but I'm still getting a syntax error.
John W. Vinson said:
I am trying to use the following statement to retrieve time card records for
a particular employee for a particular month. I keep getting an error message
that indicates Expecting Expression at the m in the DatePart function.
rstTimeCardHoursMTD.Open "SELECT TimeCardHours.CaseNum, " & _
"Sum(Hours) AS TotCaseHrsMTD, DatePart("m", DateWorked) AS [Month] "
& _
"FROM T_TimeCardHours INNER JOIN T_TimeCards " & _
"ON T_TimeCardHours.TimeCardID = T_TimeCards.TimeCardID " & _
" GROUP BY CaseNum" & _
"HAVING TimeCards.EmployeeID = lngAccessID AND DatePart("m", DateWorked) =
lngMonth"
How can I break this up so it works?
I'm sure Klatuu is right, but another thing I saw is that the doublequotes
around "m" are being interpreted as closing and opening the doublequotes
around the entire string. You're getting one string constant
"HAVING TimeCards.EmployeeID = lngAccessID AND DatePart("
followed by a meaningless "m", which Access doesn't recognize as a variable!
Try replacing "m" with 'm' (using singlequotes to ikeep from interfering with
the quoted string).
Then fix the variables as Klatuu suggests.