MS Access VBA:具有多个ID的更新循环

时间:2016-04-24 10:29:02

标签: vba ms-access-2013

我有点坚持我的项目。

目前我收到了一份表格,您可以在其中填写球员名单。 它由一个阵型(如4-3-3)组成,然后它将显示给玩家的位置,您可以从下拉列表中选择一个名称。

现在我想添加衬衫号码,但我仍然卡在那一部分。 我不知道如何更新所有匹配ID匹配我正在使用的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的表中,所有播放器详细信息都存储在该表中

TblMatchFormation

MatchFormationID(自动编号)

FormationID(获取正在播放的阵型的ID)

MatchID(获取匹配的ID)

守护者(获取作为守护者的玩家的身份)

CenterDefender(获取作为CenterDefender的玩家的ID)

tblMatchPlayer

MatchPlayerID(自动编号)

MatchID(从以前的表单中获取匹配的ID)

PlayerID(从以前的表单中获取播放器的ID)

Surname(从前一个表单中获取玩家的姓氏)

ShirtNumber(应从表格中获取数字)

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,执行此操作的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