【发布时间】:2013-11-13 17:46:45
【问题描述】:
我正在使用下面的代码在“事务表”中创建新记录,插入语句的第二行抛出错误:参数太少。我已经仔细检查了所有字段名称都是正确的。还有什么可能导致此类错误?
' Modify this line to include the path to Northwind
' on your computer.
Set dbs = CurrentDb
Dim vblCustomerID As String
Dim vblMealType As String
Dim Charge As Currency
Dim vblDate As String
vblDate = Format(Date, "yyyy-mm-dd")
txtCustomerID.SetFocus
vblCustomerID = txtCustomerID.Text
txtMealType.SetFocus
vblMealType = txtMealType.Text
txtCharge.SetFocus
vblCharge = txtCharge.Text
dbs.Execute "INSERT INTO dbo_Transactions" _
& "(CustomerID, MealID, TransactionAmount, TransactionDate) VALUES " _
& "(" & vblCustomerID & ", " & vblMealType & ", " & vblCharge & ", " & vblDate & ");"
dbs.Close
【问题讨论】:
-
执行SQL时请使用parameterized queries;否则,您将极易受到 SQL 注入攻击。这也应该可以解决您的问题。
-
我强烈建议您更改您的代码,以便您首先创建 SQL 字符串并将其分配给一个变量,然后 dbs.Execute 该变量。这样,您可以在变量处设置断点并查看 Access 认为 SQL 字符串是什么。大多数情况下,Access 认为的和您认为的完全不同。
-
你确定你的所有变量都有值吗?