Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL to VBA 2

Status
Not open for further replies.

Apollo13a

Technical User
Apr 27, 2003
277
US
Hello, I been trying to convert this sql to VBA but I keep getting an error "Syntax error in date query expression."

Code:
Set rsDays = CurDB.OpenRecordset( _
"SELECT tblhours.LaborDate, tblhours.tblEmployeeID, DatePart(#yyyy#,[labordate]) AS Expr1, tblhours.LaborHoursST" & _
" FROM tblHours" & _
" GROUP BY tblhours.LaborDate, tblhours.tblEmployeeID, DatePart(#yyyy#,[labordate]), tblhours.LaborHoursST" & _
"HAVING (((DatePart(#yyyy#,[labordate]))=DatePart(#yyyy#,Date())) AND ((tblhours.LaborHoursST)>0));")

Can anyone help me on what syntax I need around the #yyyy# ?
Do I need to place anything around the field [Labordate]?

Thanks,
Jim
 
Replace the Hash(#) marks, with double quotes.
 
Single quotes, or double up with double quotes

[tt]...DatePart('yyyy',[labordate])...
...DatePart(""yyyy"",[labordate])...[/tt]

Roy-Vidar
 
Thanks to both of you. Works great, although I have added a parameter to my query and want to pass the value thru from my function call. I am still having a little trouble with the syntax.I'm getting an error on the line that starts with "Having
Code:
Set rsDays = CurDB.OpenRecordset( _
"SELECT tblhours.LaborDate, tblhours.tblEmployeeID, DatePart('yyyy',[labordate]) AS Expr1, tblhours.LaborHoursST" & _
" FROM tblHours" & _
" GROUP BY tblhours.LaborDate, tblhours.tblEmployeeID, DatePart('yyyy',[labordate]), tblhours.LaborHoursST" & _
" HAVING (((tblHours.tblEmployeeID) = " & ID & " ) And(((DatePart('yyyy',[labordate]))=DatePart('yyyy',Date())) AND ((tblhours.LaborHoursST)>0));")
Thanks Jim
 
I really don't know, but I'll suggest this...

Set rsDays = CurDB.OpenRecordset( _
"SELECT LaborDate, tblEmployeeID, DatePart('yyyy',[labordate]) AS Expr1, LaborHoursST" & _
" FROM tblHours" & _
" GROUP BY LaborDate, tblEmployeeID, DatePart('yyyy',[labordate]), LaborHoursST" & _
" HAVING ((tblEmployeeID = " & ID & " ) And(((DatePart('yyyy',[labordate]))=DatePart('yyyy',Date())) AND ((LaborHoursST)>0));")

BTW, all the table references are not necessary, unless joining tables and 2 fields have same name.
 
Thanks Zion7
You pointed me in the right direction. The error was in the number of parenthesis.
Code:
" HAVING tblEmployeeID = " & ID & "  And (DatePart('yyyy',[labordate]) = DatePart('yyyy',Date()) AND ((LaborHoursST)>0));")
Thanks for the info on the references, certainly helps clean it up.
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top