2014-02-10 72 views
1
執行查詢

你好,我有這個疑問:錯誤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)" 

它說我說:關鍵字附近不正確的語法 「和」

+0

也許嘗試刪除這部分中的逗號(',')作爲m4,'成爲'm4'。這會觸發SQL服務器的錯誤,不確定有關ms訪問 – har07

+0

我刪除它,也試試這個eror – Sergio

+0

調試,在上面的代碼後面加上break point,並看到'strSQL'字符串輸出。如果您在查看後沒有想法,請張貼字符串。 – har07

回答

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)" 
+0

它不起作用... :( – Sergio

+0

你用這個 –

+0

得到了什麼錯誤,它告訴我他在這個修改的查詢中找不到過程錯誤 – Sergio