更新语句中的VBA语法错误

时间:2017-07-19 03:13:43

标签: vba ms-access

我知道有几个问题,但我已经看了几行这段代码,无法弄清楚问题。它一直说这行代码存在语法错误,我看不到它。任何帮助,将不胜感激。感谢。

 db.Execute "UPDATE Questions SET Date = #4/26/2014#,TwoChapt=False " & _
             " WHERE Book = '" & rs!Book & "' AND Chapter = '" & _
              rs!Chapter & "' AND QuestionType = 1 "

1 个答案:

答案 0 :(得分:1)

使用querydefs考虑parameterized queries并避免在字符串中插值:

SQL (另存为存储的Access查询对象)

PARAMETERS BookParam Text(255), ChapterParam Text(255);
UPDATE Questions 
SET [Date] = #4/26/2014#, TwoChapt = False
WHERE Book = [BookParam] AND Chapter = [ChapterParam] AND QuestionType = 1;

VBA (放置在记录集循环中,DimSet ... = Nothing除外)

...
Dim qdef As Querydef
Set qdef = Currentdb.Querydefs("MyStoredQuery")

Do While ...
  ' BIND PARAMS
  qdef!BookParam = rs!Book
  qdef!ChapterParam = rs!Chapter

  ' EXECUTE ACTION
  qdef.Execute, dbFailOnError
Loop  

Set qdef = Nothing