MS Access VBA:具有多个 ID 的更新循环
MS Access VBA : Update Loop with multiple IDs
我有点卡在我的项目上了。
目前我有一张表格,您可以在其中填写球员名单。
它由一个阵型(如 4-3-3)组成,然后它会显示球员的位置,你可以从下拉列表中 select 一个名字。
现在我也想添加球衣号码,但我卡在了那部分。
我不知道如何更新 MatchID 等于我正在处理的 MatchID 和 PlayerID 的所有玩家。
因为每个球员都有不同的球衣号码。
Option Compare Database
Private Sub Form_Load()
Me.MatchID = Me.OpenArgs
End Sub
'This Sub shows the fields where you can select the players according to the chosen formation.
Private Sub Formation_AfterUpdate()
Dim DefenderLoopVal, MidfielderLoopVal, StrikerLoopVal As String
'Get the formation from the form.
Formation = Me.Formation
'Explode the formation on the - character
FormationExploded = Split(Formation, "-")
'Put the numbers is new variables to use in the Loops.
DefenderLoopVal = FormationExploded(0)
MidfielderLoopVal = FormationExploded(1)
StrikerLoopVal = FormationExploded(2)
'MsgBox DefenderLoopVal
'MsgBox MidfielderLoopVal
'MsgBox StrikerLoopVal
'Make Keeper Visable.
Me.imgKeeper.Visible = True
Me.cbKeeper.Visible = True
Me.NrKeeper.Visible = True
'Make as many textboxes visible as necessary
For i = 1 To DefenderLoopVal
Form_frmFormation.Controls("cbDefender" & i).Visible = True
Form_frmFormation.Controls("imgDefender" & i).Visible = True
Form_frmFormation.Controls("nrDefender" & i).Visible = True
Next
For i = 1 To MidfielderLoopVal
Form_frmFormation.Controls("cbMidfielder" & i).Visible = True
Form_frmFormation.Controls("imgMidfielder" & i).Visible = True
Form_frmFormation.Controls("nrMidfielder" & i).Visible = True
Next
For i = 1 To StrikerLoopVal
Form_frmFormation.Controls("cbStriker" & i).Visible = True
Form_frmFormation.Controls("imgStriker" & i).Visible = True
Form_frmFormation.Controls("nrStriker" & i).Visible = True
Next
End Sub
'This is the actual saving Sub, it will save the players on the according positions
Private Sub Save_Formation_Click()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("tblMatchFormation", dbOpenDynaset, dbAppendOnly)
rs.AddNew
rs!MatchID = Me!MatchID
rs!FormationID = Me!Formation
rs!Keeper = Me!cbKeeper
rs!CenterDefender = Me!cbDefender1
rs!CenterRightDefender = Me!cbDefender2
rs!CenterLeftDefender = Me!cbDefender3
rs!LeftDefender = Me!cbDefender4
rs!RightDefender = Me!cbDefender5
rs!CenterMidfielder = Me!cbMidfielder1
rs!CenterRightMidfielder = Me!cbMidfielder2
rs!CenterLeftMidfielder = Me!cbMidfielder3
rs!LeftMidfielder = Me!cbMidfielder4
rs!RightMidfielder = Me!cbMidfielder5
rs!CenterStriker = Me!cbStriker1
rs!RightStriker = Me!cbStriker2
rs!LeftStriker = Me!cbStriker3
rs.Update
'Should have a update query here that updates the tblMatchPlayer with the numbers according to the MatchID and PlayerID
End Sub
但现在我也想添加球员号码,该字段在另一个名为 tblMatchPlayer 的 table 上,所有球员详细信息都存储在 table
TblMatchFormation
MatchFormationID(自动编号)
FormationID(获取正在播放的编队ID)
MatchID(获取匹配的 ID)
Keeper(获取作为 Keeper 的玩家的 ID)
CenterDefender(获取 CenterDefender 玩家的 ID)
等等
tblMatchPlayer
MatchPlayerID(自动编号)
MatchID(从之前的表格中获取匹配的 ID)
PlayerID(从之前的表单中获取玩家的ID)
姓氏(从上一个表格中获取玩家的姓氏)
ShirtNumber(应从表格中获取号码)
执行此操作的 SQL 语句,如果我理解正确的话,将是:
UPDATE tblMatchPlayer SET ShirtNumber = <shirt number>
WHERE MatchID = <matchID> AND PlayerID = <playerID>;
或在 VB 中构建为:
Dim strSQL As String
strSQL= "UPDATE tblMatchPlayer SET ShirtNumber = '" & shirt_number & "' " & _
"WHERE MatchID = '" & matchID & "' AND PlayerID = '" & playerID &"';"
执行查询:
dbs.Execute strSQL
我有点卡在我的项目上了。
目前我有一张表格,您可以在其中填写球员名单。 它由一个阵型(如 4-3-3)组成,然后它会显示球员的位置,你可以从下拉列表中 select 一个名字。
现在我也想添加球衣号码,但我卡在了那部分。 我不知道如何更新 MatchID 等于我正在处理的 MatchID 和 PlayerID 的所有玩家。 因为每个球员都有不同的球衣号码。
Option Compare Database
Private Sub Form_Load()
Me.MatchID = Me.OpenArgs
End Sub
'This Sub shows the fields where you can select the players according to the chosen formation.
Private Sub Formation_AfterUpdate()
Dim DefenderLoopVal, MidfielderLoopVal, StrikerLoopVal As String
'Get the formation from the form.
Formation = Me.Formation
'Explode the formation on the - character
FormationExploded = Split(Formation, "-")
'Put the numbers is new variables to use in the Loops.
DefenderLoopVal = FormationExploded(0)
MidfielderLoopVal = FormationExploded(1)
StrikerLoopVal = FormationExploded(2)
'MsgBox DefenderLoopVal
'MsgBox MidfielderLoopVal
'MsgBox StrikerLoopVal
'Make Keeper Visable.
Me.imgKeeper.Visible = True
Me.cbKeeper.Visible = True
Me.NrKeeper.Visible = True
'Make as many textboxes visible as necessary
For i = 1 To DefenderLoopVal
Form_frmFormation.Controls("cbDefender" & i).Visible = True
Form_frmFormation.Controls("imgDefender" & i).Visible = True
Form_frmFormation.Controls("nrDefender" & i).Visible = True
Next
For i = 1 To MidfielderLoopVal
Form_frmFormation.Controls("cbMidfielder" & i).Visible = True
Form_frmFormation.Controls("imgMidfielder" & i).Visible = True
Form_frmFormation.Controls("nrMidfielder" & i).Visible = True
Next
For i = 1 To StrikerLoopVal
Form_frmFormation.Controls("cbStriker" & i).Visible = True
Form_frmFormation.Controls("imgStriker" & i).Visible = True
Form_frmFormation.Controls("nrStriker" & i).Visible = True
Next
End Sub
'This is the actual saving Sub, it will save the players on the according positions
Private Sub Save_Formation_Click()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("tblMatchFormation", dbOpenDynaset, dbAppendOnly)
rs.AddNew
rs!MatchID = Me!MatchID
rs!FormationID = Me!Formation
rs!Keeper = Me!cbKeeper
rs!CenterDefender = Me!cbDefender1
rs!CenterRightDefender = Me!cbDefender2
rs!CenterLeftDefender = Me!cbDefender3
rs!LeftDefender = Me!cbDefender4
rs!RightDefender = Me!cbDefender5
rs!CenterMidfielder = Me!cbMidfielder1
rs!CenterRightMidfielder = Me!cbMidfielder2
rs!CenterLeftMidfielder = Me!cbMidfielder3
rs!LeftMidfielder = Me!cbMidfielder4
rs!RightMidfielder = Me!cbMidfielder5
rs!CenterStriker = Me!cbStriker1
rs!RightStriker = Me!cbStriker2
rs!LeftStriker = Me!cbStriker3
rs.Update
'Should have a update query here that updates the tblMatchPlayer with the numbers according to the MatchID and PlayerID
End Sub
但现在我也想添加球员号码,该字段在另一个名为 tblMatchPlayer 的 table 上,所有球员详细信息都存储在 table
TblMatchFormation
MatchFormationID(自动编号)
FormationID(获取正在播放的编队ID)
MatchID(获取匹配的 ID)
Keeper(获取作为 Keeper 的玩家的 ID)
CenterDefender(获取 CenterDefender 玩家的 ID)
等等
tblMatchPlayer
MatchPlayerID(自动编号)
MatchID(从之前的表格中获取匹配的 ID)
PlayerID(从之前的表单中获取玩家的ID)
姓氏(从上一个表格中获取玩家的姓氏)
ShirtNumber(应从表格中获取号码)
执行此操作的 SQL 语句,如果我理解正确的话,将是:
UPDATE tblMatchPlayer SET ShirtNumber = <shirt number>
WHERE MatchID = <matchID> AND PlayerID = <playerID>;
或在 VB 中构建为:
Dim strSQL As String
strSQL= "UPDATE tblMatchPlayer SET ShirtNumber = '" & shirt_number & "' " & _
"WHERE MatchID = '" & matchID & "' AND PlayerID = '" & playerID &"';"
执行查询:
dbs.Execute strSQL