2016-04-24 32 views
0

我有點卡在我的項目上。MS Access VBA:更新多個ID的循環

目前我有一個表格,你可以填寫球員名冊。 它由陣型組成(比如4-3-3),然後它會顯示給玩家的位置,你可以從下拉列表中選擇一個名字。

現在我想添加球衣號碼,但是我被卡住了。 我不知道如何更新所有的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,所有的播放器詳細信息被存儲在該表中

TblMatchFormation

一個其他表

MatchFormationID(自動編號)

FormationID(獲取所播放的形成的ID)

MatchID(獲取匹配的ID)

守護者(獲取這是一個守護者的玩家ID)

CenterDefender(獲取這是一個CenterDefender的玩家ID)

tblMatchPlayer

MatchPlayerID(自動編號)

MatchID(獲取匹配的ID從以前的形式)

PlayerID(從先前的形式獲取的玩家的ID)

姓(獲取播放器的姓形成以前形式)

ShirtNumber(應該得到表格中的數字)

也許你們可以幫我嗎?

隨着親切的問候, 帕特里克

回答

0

的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 
+0

謝謝,這就是我需要的 – PatrickStel