在VBA中执行查询时出错

时间:2014-02-10 06:55:50

标签: sql vba access-vba

您好我有这个问题:

strSQL =    "SELECT DATEPART(yyyy,ArriveDate) AS God," & _

"COUNT (CASE  DATEPART(d,ArriveDate) WHEN 1 THEN DATEPART(d,ArriveDate) END)As m1 ," & _
"COUNT (CASE  DATEPART(d,ArriveDate) WHEN 2 THEN DATEPART(d,ArriveDate) END)As m2 ," & _
"COUNT (CASE  DATEPART(d,ArriveDate) WHEN 3 THEN DATEPART(d,ArriveDate) END)As m3 ," & _
"COUNT (CASE  DATEPART(d,ArriveDate) WHEN 4 THEN DATEPART(d,ArriveDate) END)As m4 " & _

 " FROM Company " & _

  " WHERE DATEPART(yyyy,ArriveDate) =" & [Forms]![Raport_lunar_anual_carnete]![ANUL] & _

  " AND DATEPART(mm,ArriveDate)= " & [Forms]![Raport_lunar_anual_carnete]![LUNA] & _

  " GROUP BY DATEPART(yyyy,ArriveDate)"

它告诉我:关键字“AND”附近的语法错误

1 个答案:

答案 0 :(得分:0)

试试这个

strSQL =          " SELECT DATEPART(yyyy,ArriveDate) AS God,"    
strSQL = strSQL & "   COUNT (CASE  DATEPART(d,ArriveDate) WHEN 1 THEN DATEPART(d,ArriveDate) END)As m1 ,"
strSQL = strSQL & "   COUNT (CASE  DATEPART(d,ArriveDate) WHEN 2 THEN DATEPART(d,ArriveDate) END)As m2 ," 
strSQL = strSQL & "   COUNT (CASE  DATEPART(d,ArriveDate) WHEN 3 THEN DATEPART(d,ArriveDate) END)As m3 ," 
strSQL = strSQL & "   COUNT (CASE  DATEPART(d,ArriveDate) WHEN 4 THEN DATEPART(d,ArriveDate) END)As m4 ,"    
strSQL = strSQL & " FROM Company "     
strSQL = strSQL & " WHERE DATEPART(yyyy,ArriveDate) =" & [Forms]![Raport_lunar_anual_carnete]![ANUL] &     
strSQL = strSQL & " AND DATEPART(mm,ArriveDate)= " & [Forms]![Raport_lunar_anual_carnete]![LUNA] &     
strSQL = strSQL & " GROUP BY DATEPART(yyyy,ArriveDate)"