运行时3134错误MS Access SQL

时间:2016-06-11 15:42:09

标签: sql ms-access access-vba insert-into

Private Sub AddItemButton_Click()
    Dim db As Database
    Set db = CurrentDb
    Dim Item As String
    Dim Rate As String
    Dim AltRate As String
    Dim Reason As String
    Dim ApporvedBy As String
    Dim Company As String
    Dim JobID As String

        Me.JobID2 = Me.JobID

        db.Execute "INSERT INTO InvItemsRecords (Item, Rate, AltRate, Reason, ApprovedBy, Company, JobID) VALUES ('" & Me.ItemAppendCBO & "', " & Me.RateAppend & ", " & Me.AltRateAppend & ", '" & Me.ReasonAppend & "', '" & Me.ApprovedByAppend & "', '" & Me.Customer & "', " & Me.JobID2 & ")"
        Me.AltRate2.Value = Null

End Sub

1 个答案:

答案 0 :(得分:0)

您应该在查询中使用字符串文字

Dim sqlQuery As String
    sqlQuery = "INSERT INTO InvItemsRecords (Item, Rate, AltRate, Reason, ApprovedBy, " & _
               "Company, JobID) VALUES ('" & Me.ItemAppendCBO & "', " & Me.RateAppend & ", " & Me.AltRateAppend & ", '" & Me.ReasonAppend & "', '" & _
               Me.ApprovedByAppend & "', '" & Me.Customer & "', " & Me.JobID2 & ")"

db.Execute sqlQuery

使调试更容易

INSERT INTO InvItemsRecords (Item, Rate, AltRate, Reason, ApprovedBy, Company, JobID) 
 VALUES('Some Item', 100, 343, 'Reason A', 'John', 'Jane Doe', Null)

您可能无意中将1个或多个空值传递给不可为空的字段。还要注意数据类型。您可能需要使用CStr(),CInt(),CLong,CDate()或...将数据转换为正确的格式如果字段可以留空我们NZ()。

NZ(Me.RateAppend,True)