下午好,
我在VBA中遇到INSERT语句问题。 我每次尝试通过表单中的输入向表中插入新记录时都会收到错误。
运行时错误' 3061': 参数太少。预计2。
这就是它背后的VBA。
Private Sub Command242_Click()
Dim dbs As Database
' Modify this line to include the path to Northwind
' on your computer.
Set dbs = CurrentDb
'Testing purpose
Me.cbPenalty1 = 0
Me.cbOwnGoal1 = 0
' Create a new record in the tblMatchPlayer table.
' Query saved the player who scored with values in a new row, linked with MatchID & PlayerID.
'Testing purpose MsgBox - DELETE WHEN WORKS!!
MsgBox " INSERT INTO tblMatchPlayer " _
& "(MatchID, PlayerID, SubstituteID, PositionID, Surname, ScoreTime, RedCards, YellowCards, Substitude, Penalty, OwnGoal, Assist) VALUES " _
& "(" & Me.MatchID & ", '', '', '', " & Me.cmScoreName1 & ", " & Me.tbScoreTime1 & ", '', '', '', " & Me.cbPenalty1 & ", " & Me.cbOwnGoal1 & ", " & Me.cmAssist1 & ");", vbOKOnly, "Query Show"
'Actual INSERT
dbs.Execute " INSERT INTO tblMatchPlayer " _
& "(MatchID, PlayerID, SubstituteID, PositionID, Surname, ScoreTime, RedCards, YellowCards, Substitude, Penalty, OwnGoal, Assist) VALUES " _
& "(" & Me.MatchID & ", '', '', '', " & Me.cmScoreName1 & ", " & Me.tbScoreTime1 & ", '', '', '', " & Me.cbPenalty1 & ", " & Me.cbOwnGoal1 & ", " & Me.cmAssist1 & ");"
dbs.Close
End Sub
当MsgBox弹出窗口向我显示查询时,它会在表格中写下我得到这些结果。
INSERT INTO tblMatchPlayer(MatchID,PlayerID,substituteID,PositionID,Surname,ScoreTime,RedCards,YellowCards,Substitude,Penalty,OwnGoal,Assist)VALUES(29,'','&# 39;,'',Grozema,34,''''',0,0,Bruins);
我看不出这个插入查询有什么问题。但是VBA似乎认为他缺少一些参数,但我不知道参数是什么。
我表格中的字段就是这样。
你们可以帮助我吗?
亲切的问候, 帕特里克
答案 0 :(得分:0)
而不是在MsgBox中查看sql语句,而是调试它。然后复制它,创建一个新查询,切换到SQL视图并将该语句粘贴为SQL。切换到设计视图,问题将使其处于领先地位。