您需要[]
在任何表或字段名称周围加上方括号
试试这个:
CurrentDb.Execute "INSERT INTO PlantTransactionQuery (TransactionID,[Plant Number],Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,[Hours Worked],Fuel,[Fuel Cons Fuel/Hours],[Hour Meter Replaced],Comments) " & _
"VALUES (" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
编辑
如果您将 SQL 语句逐行分解,则可以使 SQL 语句更易于直观地验证,如下所示:
CurrentDb.Execute _
"INSERT INTO [PlantTransactionQuery] (" & _
"[TransactionID], " & _
"[Plant Number], " & _
"[Categories], " & _
"[Description], " & _
"[Location], " & _
"[TransactionDate], " & _
"[Opening_Hours], " & _
"[Closing_Hours], " & _
"[Hours Worked], " & _
"[Fuel], " & _
"[Fuel Cons Fuel/Hours], " & _
"[Hour Meter Replaced], " & _
"[Comments] " & _
") VALUES (" & _
Me.txt13 & ", " & _
"'" & Me.txt1 & "', " & _
"'" & Me.txt2 & "', " & _
"'" & Me.txt3 & "', " & _
"'" & Me.txt4 & "', " & _
"#" & Me.txt5 & "#, " & _
"'" & Me.txt6 & "', " & _
"'" & Me.txt7 & "', " & _
"'" & Me.txt8 & "', " & _
"'" & Me.txt9 & "', " & _
"'" & Me.txt10 & "', " & _
Me.txt11 & ", " & _
Me.txt12 & " " & _
")"
如果字段(列)名称拼写错误,或者恰好是保留字的字段名称未包含在方括号中,则可能会出现 3061 错误消息“参数太少...”。我已经编辑了上面的查询,所以为了安全起见,所有表名和列名都用括号括起来。
另外,请检查您是否引用了字符串而不是引用数字。那里的错误也可能导致上述错误。
至于 [TransactionDate],您可能应该取消绑定文本框以保持一致性。请注意,该字段的值用井#
号 ( ) 而非单引号括起来。您可以在文本框中添加“日期”输入掩码,或者您可以使用类似...
Format(CDate(Me.txt5), "yyyy-mm-dd")
...为您格式化日期。
最后,请注意,构建这样的 SQL 字符串会使您面临SQL 注入问题,因此您也可以考虑将其替换.Execute
为类似
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("[PlantTransactionQuery]", dbOpenTable)
rst.AddNew
rst![TransactionID] = CLng(Me.txt13)
rst![Plant Number] = Me.txt1
rst![Categories] = Me.txt2
...
rst![Comments] = Me.txt12
rst.Update