2017-10-11 83 views
0

下面的代碼返回Access VBA中的語法錯誤,我在查詢設計中使用了相同的代碼,這種代碼工作正常。我知道在查詢設計模式與VBA查詢時,SQL代碼可能會有所不同,但我無法確定如何使Access代碼VBA中的代碼無誤地運行。VBA訪問中此SQL語句的語法有什麼問題?

SELECT TOP 1 
    MAX(CurrentWeek.WeekEnding) AS MaxOfWeekEnding, 
    CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan 
FROM 
    CurrentWeek 
INNER JOIN 
    (tblNWABasic 
INNER JOIN 
    tblProjects ON tblNWABasic.ProjectID = tblProjects.ProjectID) ON CurrentWeek.NWA = tblNWABasic.NWA 
GROUP BY 
    CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan 
HAVING 
    (((CurrentWeek.[NWA Description]) Like "*direct cite*")) 
ORDER BY 
    MAX(CurrentWeek.WeekEnding) DESC; 
+0

是'CurrentWeek'一個表或查詢?如果是查詢,我們需要看到查詢的定義,因爲它可能實際上導致問題。 – this

+0

CurrentWeek是一張桌子。我想通了,我要調整代碼。 – delnav222

回答

1

直接引用部分丟失括號。如下圖所示修正這段代碼:

Like " * [direct cite] * ")) 

上面的代碼應該已經工作,但實際上結束了什麼工作是:

Like ' *[direct cite]* ')) 
+0

這沒有任何意義。它在一個常量字符串中 - Access不會在乎括號(至少它不會導致語法錯誤)。 – Andre

1

您的代碼應該在雙引號"不引用文字「 「包圍。您還需要通過他們,在查詢加倍逃避所有雙引號(如改變"*direct cite*"""*direct cite*""

SELECT TOP 1 Max(CurrentWeek.WeekEnding) AS MaxOfWeekEnding, CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan FROM CurrentWeek INNER JOIN (tblNWABasic INNER JOIN tblProjects ON tblNWABasic.ProjectID = tblProjects.ProjectID) ON CurrentWeek.NWA = tblNWABasic.NWA GROUP BY CurrentWeek.NWA, CurrentWeek.[NWA Description], CurrentWeek.Plan HAVING (((CurrentWeek.[NWA Description]) Like ""*direct cite*"")) ORDER BY Max(CurrentWeek.WeekEnding) DESC; 

這個子程序將簡化逃逸引號的過程。簡單的字符串複製到Windows剪貼板,運行EscapeDoubleQuotesInClipboard和新的字符串粘貼到您的代碼。

Sub EscapeDoubleQuotesInClipboard() 
    With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 
     .GetFromClipboard 
     .SetText Replace(.GetText, Chr(34), Chr(34) & Chr(34)) 
     .PutInClipboard 
    End With 
End Sub