我在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,姓,ScoreTime,RedCards,YellowCards,分別代替,罰金,OwnGoal,輔助)VALUES(29, '', '', '',Grozema,34 ,'','','',0,0,Bruins);
我看不出這個插入查詢有什麼問題,但是VBA似乎認爲他缺少一些參數,但我不知道什麼參數。
我的表中的字段是這樣的。
- MatchPlayerID - 自動編號
- MatchID - 數字
- PlayerID - 數字
- SubstituteID - 數字
- PositionID - 數字
- 姓 - 文本
- ScoreTime - 文本
- RedCards - 文字
- 個YellowCards - 文本
- 分別代替 - 文本
- 懲罰 - 是/否
- OwnGoal - 是/否
- ASSIST - 已文本
你們可以幫助我嗎?
隨着親切的問候, 帕特里克
它接受空值嗎? –
你的意思是空的領域?或者你是指「是/否」字段? – PatrickStel
您可能會對此字段進行拼寫錯誤:「OwnGoals」。在你的查詢中你有'OwnGoal' - 沒有s。此外,我不確定是否可以將空字符串'''作爲'PlayerID'字段賦予,如果它是Number類型的(與其他字段相同,即'SubstituteID') - 可以嘗試將其更改爲0而不是空字符串。 – mielk