vba 访问运行时错误“3061”:参数太少。预计 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22184774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Run-time error '3061': Too few parameters. Expected 1
提问by user3377449
I'm getting an Access Run-time error:
我收到 Access 运行时错误:
'3061': Too few parameters. Expected 1
'3061':参数太少。预计 1
when trying to run some VBA code from a form with sub-report.
尝试从带有子报表的表单运行一些 VBA 代码时。
I've tried with the variable (CurAssetID
) as well as the direct link (Forms!Details!ID
) in the SQL query but both result in the same error. I put in the MsgBox just to verify it was picking up the correct value which it is.
我已经尝试在 SQL 查询中使用变量 ( CurAssetID
) 以及直接链接 ( Forms!Details!ID
) 但两者都导致相同的错误。我放入 MsgBox 只是为了验证它是否选择了正确的值。
If I replace the last part of the SQL query with a value (HAVING (((Assignments.AssetID)=1));"
) it works fine. What's going on here and how can I fix it?
如果我用值 ( HAVING (((Assignments.AssetID)=1));"
)替换 SQL 查询的最后一部分,它工作正常。这里发生了什么,我该如何解决?
Private Sub LineSelect_Click()
CurAssetID = Forms!Details!ID
Status = MsgBox(CurAssetID, vbOKOnly)
Dim LastAssignment As DAO.Recordset
LastAssignmentSQL = "SELECT Assignments.AssetID, Last(Assignments.LocationID) AS LastLocationID FROM Assignments GROUP BY Assignments.AssetID HAVING (((Assignments.AssetID)=CurAssetID));"
Set LastAssignment = CurrentDb.OpenRecordset(LastAssignmentSQL, dbOpenDynaset, dbSeeChanges)
采纳答案by Barranka
You can create a string variable with the parameter value concatenated inside it:
您可以创建一个字符串变量,并在其中连接参数值:
curAssetID = Forms!Details!ID
status = MsgBox(CurAssetID, vbOKOnly)
Dim lastAssignment As DAO.Recordset
lastAssignmentSQL = "SELECT Assignments.AssetID, " & _
"Last(Assignments.LocationID) AS LastLocationID " & _
"FROM Assignments " & _
"GROUP BY Assignments.AssetID " & _
"HAVING (((Assignments.AssetID)=" & CurAssetID & "));"
Set lastAssignment = CurrentDb.OpenRecordset(lastAssignmentSQL, dbOpenDynaset, dbSeeChanges)
' ...
As an additional precaution, if Forms!Details!ID
is suposed to be a number (Integer
or Long
), I suggest you declare the variable CurAssetID
explicitly:
作为额外的预防措施,如果Forms!Details!ID
s suposed 是一个数字(Integer
或Long
),我建议您CurAssetID
明确声明该变量:
Dim curAssetID as Integer ' Or Long
curAssetID = Forms!Details!ID
Dim lastAssignment As DAO.Recordset
lastAssignmentSQL = "SELECT Assignments.AssetID, " & _
"Last(Assignments.LocationID) AS LastLocationID " & _
"FROM Assignments " & _
"GROUP BY Assignments.AssetID " & _
"HAVING (((Assignments.AssetID)=" & CurAssetID & "));"
Set lastAssignment = CurrentDb.OpenRecordset(lastAssignmentSQL, dbOpenDynaset, dbSeeChanges)
' ...
If the value is a 'String', then you should enclose the value in quotes in your query:
如果该值为“字符串”,则应在查询中将该值括在引号中:
Dim curAssetID as String
' ...
lastAssignmentSQL = "SELECT Assignments.AssetID, " & _
"Last(Assignments.LocationID) AS LastLocationID " & _
"FROM Assignments " & _
"GROUP BY Assignments.AssetID " & _
"HAVING (((Assignments.AssetID)= '" & CurAssetID & "'));"
' ...