2017-08-09 67 views
0

幫助,我想與現場PARKING_STATUS價值修訂來更新我的表停車但我得到的錯誤。我從同一張表中獲取所有內容。表格中的所有字段均來自同一張表PARKING,PK和FK與其他表格。這是代碼:訪問VBA更新一個表中,多個字段

Private Sub Approve_Btn_Click() 

    'update data into table(s) 
    CurrentDb.Execute "UPDATE PARKING" 
    "SET(parking_id= '" & Me.lot_num_dropdown & "', parking_status = '" & "UPDATED" & "', car_reg_num = '" & Me.car_reg_num_dropdown & "', result_id = '" & Me.result_id_dropdown & "')" & _ 
    "WHERE parking_id = '" & Me.lot_num_dropdown & "'" 

End Sub 
+1

在SQL命令'SET'和'WHERE'之前和之後需要空間。 – Parfait

回答

1

考慮使用querydef parameterziation更安全,更乾淨,更易維護的版本。不需要引號或字符串連接。

只需將參數值綁定到準備好的SQL語句,該準備好的SQL語句也可以是數據庫中保存的查詢而不是VBA字符串。

Private Sub Approve_Btn_Click() 
    Dim qdef As QueryDef 

    sql = "PARAMETERS idparam LONG, statusparam TEXT(255), carregparam TEXT(255), resultidparam LONG;" _ 
      & " UPDATE PARKING SET parking_id = [idparam], parking_status = [statusparam]," _ 
      & "     car_reg_num = [carregparam], result_id = [resultidparram]" _ 
      & " WHERE parking_id = [idparam]" 

    ' assign prepared statement to qdef object 
    Set qdef = CurrentDb.CreateQueryDef("", sql) 'OR Set qdef = CurrentDb.QueryDefs("mySavedQuery") 

    ' bind named parameters 
    qdef!idparam = Me.lot_num_dropdown 
    qdef!statusparam = "UPDATED" 
    qdef!carregparam = Me.car_reg_num_dropdown 
    qdef!resultidparam = Me.result_id_dropdown 

    ' execute action query 
    qdef.Execute dbFailOnError 

    Set qdef = Nothing 
End Sub 
相關問題