1

嘿,美好的一天,有人可以帮我吗?我有一个主窗体和一个子窗体,我创建了一个查询来连接这两个表,并且我尝试使用该查询来插入未绑定的字段,如果我插入到我的字段中,我的子窗体字段会从我的连接查询中得到它说和单击添加按钮,我收到一条错误消息:运行时错误“3134”:插入语句中的语法错误。

我在下面使用的代码:

Private Sub Add_Click() 
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 & ")"
PlantTransactionsubform.Form.Requery 
End Sub

我不知道您是否可以插入查询中,并且我不知道我必须将哪个名称放在保留名称的括号中。任何帮助将不胜感激如何在同一页面上的按钮 onclick 中的子表单中添加两个表,甚至可能我在代码中犯了一个错误,对我来说一切看起来都很好。提前致谢

4

1 回答 1

0

您需要[]在任何表或字段名称周围加上方括号

试试这个:

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
于 2013-05-08T10:45:13.553 回答