我遇到了Access Run-time错误:
'3061':参数太少。预计1
尝试从包含子报表的表单运行某些VBA代码时。
我已尝试使用变量(CurAssetID
)以及SQL查询中的直接链接(Forms!Details!ID
),但两者都会导致相同的错误。我放入MsgBox只是为了验证它是否正在拾取正确的值。
如果我用值(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)
答案 0 :(得分:0)
您可以创建一个字符串变量,其中包含参数值:
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)
' ...
作为额外的预防措施,如果Forms!Details!ID
被认为是一个数字(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)
' ...
如果值为'String',则应在查询中将值括在引号中:
Dim curAssetID as String
' ...
lastAssignmentSQL = "SELECT Assignments.AssetID, " & _
"Last(Assignments.LocationID) AS LastLocationID " & _
"FROM Assignments " & _
"GROUP BY Assignments.AssetID " & _
"HAVING (((Assignments.AssetID)= '" & CurAssetID & "'));"
' ...