2014-10-06 37 views
0

我得到了更新statement.Run時錯誤語法錯誤:3144 我用下面的代碼語法錯誤3144

CurrentDb.Execute "UPDATE product " & _ 
    " SET [product name] = '" & Me.txtName & "'" & _ 
    ", [cost of product] = " & Me.txtCost & "" & _ 
    ", [weight] = " & Me.txtWeight & "" & _ 
    ", [group] = '" & Me.CmbGroup & "'" & _ 
    ", [group ID] = '" & Me.txtGroupID & "'" & _ 
    ", [Ordered] = " & Me.txtOrdered & "" & _ 
    " WHERE [roduct name] = " & Me.txtName.Tag & "" 

可以採取什麼問題嗎? 如果有道理,那麼Me.txtCost,Me.txtWeight和me.txtOrdered的號碼是

感謝您的幫助!

+1

這看起來錯誤: 'WHERE [roduct name]'應該是'[產品名稱]'而不是? Me.txtCost,Me.txtWeight,Me.txtOrdered或Me.txtName.Tag可以爲空嗎? – HansUp 2014-10-06 19:41:00

+1

使用字符串變量* strUpdate *來保存您的'UPDATE'語句。然後使用'Debug.Print strUpdate'來查看你要求數據庫引擎執行的內容。你可以用'CurrentDb.Execute strUpdate'執行它。 – HansUp 2014-10-06 19:43:07

+0

嗨。感謝您的快速幫助!這就是問題,迷惑......煩人..但是我還有一個問題,如果你不把數據放在一個字段中,是否有代碼要忽略?由於此代碼僅適用於填寫所有字段,否則我收到錯誤。謝謝! – 2014-10-07 20:31:29

回答

2

兩個問題,我看到:

  1. 錯字在WHERE [roduct name](應該是WHERE [product name]
  2. 各地Me.txtName.Tag缺少報價在聲明

結束試試這個:

CurrentDb.Execute "UPDATE product " & _ 
    " SET [product name] = '" & Me.txtName & "'" & _ 
    ", [cost of product] = " & Me.txtCost & "" & _ 
    ", [weight] = " & Me.txtWeight & "" & _ 
    ", [group] = '" & Me.CmbGroup & "'" & _ 
    ", [group ID] = '" & Me.txtGroupID & "'" & _ 
    ", [Ordered] = " & Me.txtOrdered & "" & _ 
    " WHERE [product name] = '" & Me.txtName.Tag & "'" 
+0

嗨。感謝您的快速幫助!這就是問題,迷惑......煩人..但是我還有一個問題,如果你不把數據放在一個字段中,是否有代碼要忽略?由於此代碼僅適用於填寫所有字段,否則我收到錯誤。謝謝! – 2014-10-07 20:28:31

0

給未來的讀者發佈後,重新考慮將VBA值插入或連接成動態SQL查詢。考慮parameterized queries與MS Access'QueryDefs。這將避免錯誤,錯誤引用,不可讀,不可維護的代碼。

SQL(保存爲只有一次一個MS Access存儲查詢)

PARAMETERS [txtName_PARAM] TEXT, [txtCost_PARAM] DOUBLE, 
      [txtWeight_PARAM] DOUBLE, [CmbGroup_PARAM] TEXT, 
      [txtGroupID_PARAM] TEXT, [txtOrdered_PARAM] LONG, 
      [txtName_Tag_PARAM] TEXT; 
UPDATE product 
SET [product name] = [txtName_PARAM], 
    [cost of product] = [txtCost_PARAM], 
    [weight] = [txtWeight_PARAM], 
    [group] = [CmbGroup_PARAM], 
    [group ID] = [txtGroupID_PARAM], 
    [Ordered] = [txtOrdered_PARAM], 
    WHERE [product name] = [txtName_Tag_PARAM]; 

VBA(動態綁定值參數佔位符)

Dim qdef as QueryDef 

Set qdef = CurrentDb.QueryDefs("mySavedQuery") 

qdef![txtName_PARAM] = Me.txtName 
qdef![txtCost_PARAM] = Me.txtCost 
qdef![txtWeight_PARAM] = Me.txtWeight 
qdef![CmbGroup_PARAM] = Me.CmbGroup 
qdef![txtGroupID_PARAM] = Me.txtGroupID 
qdef![txtOrdered_PARAM] = Me.txtOrdered 
qdef![txtName_Tag_PARAM] = Me.txtName.Tag 

qdef.Execute dbFailOnError 

Set qdef = Nothing