【发布时间】:2015-12-18 11:44:43
【问题描述】:
下午好,
我在使用 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 弹出窗口向我显示它将在表中写入的查询时,我会得到这些结果。
插入到 tblMatchPlayer(MatchID、PlayerID、substituteID、PositionID、Surname、ScoreTime、RedCards、YellowCards、Substitude、点球、OwnGoal、Assist)值(29、''、''、''、Grozema、34、'' , '', '', 0, 0, 熊);
我看不出这个插入查询有什么问题。但是 VBA 似乎认为他缺少一些参数,但我不知道是什么参数。
我表中的字段是这样的。
- MatchPlayerID - 自动编号
- MatchID - 号码
- 玩家 ID - 号码
- SubstituteID - 号码
- PositionID - 编号
- 姓氏 - 文字
- ScoreTime - 文字
- 红卡 - 文字
- 黄卡 - 文字
- 替换 - 文本
- 处罚 - 是/否
- OwnGoal - 是/否
- 辅助 - 文字
你们能帮帮我吗?
诚挚的问候, 帕特里克
【问题讨论】:
-
是否接受空值?
-
你的意思是空字段吗?还是您的意思是“是/否”字段?
-
您可能在此字段中打错了字:
OwnGoals。在您的查询中,您有OwnGoal- 没有 s。另外,如果它是数字类型(与其他字段相同,即SubstituteID),我不确定您是否可以将空字符串''作为PlayerID字段 - 也许尝试将其更改为 0 而不是空字符串。 -
对不起,我在这里填写时打错了。这都是 OwnGoal 不高兴,即使我给这些字段的值 0 它一直说同样的错误。 :(
-
另一件事是最后一个值:
Bruins应该在引号中。
标签: vba ms-access insert-query