2016-04-24 7 views
0

私はちょっと私のプロジェクトに詰まっています。MS Acces 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(フォームのフォーム番号を取得する必要があります)

たぶん君たちは私を助けることができますか?

UPDATE tblMatchPlayer SET ShirtNumber = <shirt number> 
WHERE MatchID = <matchID> AND PlayerID = <playerID>; 

かのようにVBでそれを構築する:種類に関しては

答えて

0

パトリックこれを行うには、SQL文、私が正しくあなたを理解していれば、だろう

Dim strSQL As String 
strSQL= "UPDATE tblMatchPlayer SET ShirtNumber = '" & shirt_number & "' " & _ 
     "WHERE MatchID = '" & matchID & "' AND PlayerID = '" & playerID &"';" 

クエリを実行する:

dbs.Execute strSQL 
+0

ありがとう、それは私が必要なものです – PatrickStel

関連する問題