Hiya, please can you help:

In my poker program I used to have loads of code for the 10 player slots, I used to ElseIf through each player in each player spot. Tonnes of code.

Now I want to loop through so I have only one set of code. The dropdown Boxes are called:
Player1, Player2, Player3, Player4, Player5, Player6, Player7, Player8, Player9, Player10

The problem I have is the correct code to replace the number after the word PLAYER.
ie: If Player(PlayX) = "YT" Then

There seems to be some syntax problem with the way I'm doing the Player(PlayX) bit.

Dim (PlayX) As Integer
For PlayX = 1 To 10

If Player(PlayX) = "YT" Then
Open "C:\Poker\yt.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\yt.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image3.Picture = Image4

ElseIf Player(PlayX) = "Morbid" Then
Open "C:\Poker\morbid.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\morbid.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image5.Picture = Image6

ElseIf Player(PlayX) = "Big Head" Then
Open "C:\Poker\bighead.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\bighead.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image20.Picture = Image19

ElseIf Player(PlayX) = "Woody" Then
Open "C:\Poker\woody.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\woody.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image7.Picture = Image8

ElseIf Player(PlayX) = "Dog" Then
Open "C:\Poker\dog.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\dog.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Darryl" Then
Open "C:\Poker\darryl.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\darryl.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image23.Picture = Image22

ElseIf Player(PlayX) = "E Honda" Then
Open "C:\Poker\ehonda.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\ehonda.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Bandit" Then
Open "C:\Poker\bandit.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\bandit.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image14.Picture = Image13

ElseIf Player(PlayX) = "Dyksey" Then
Open "C:\Poker\dyksey.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\dyksey.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Lucock" Then
Open "C:\Poker\lucock.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\lucock.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image10.Picture = Image9

ElseIf Player(PlayX) = "Stonch" Then
Open "C:\Poker\stonch.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\stonch.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Jason N" Then
Open "C:\Poker\jasonn.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\jasonn.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image34.Picture = Image35

ElseIf Player(PlayX) = "Jonsey" Then
Open "C:\Poker\jonsey.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\jonsey.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
Image26.Picture = Image24

ElseIf Player(PlayX) = "John D" Then
Open "C:\Poker\johnd.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\johnd.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Steve M" Then
Open "C:\Poker\stevem.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\stevem.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Hooter" Then
Open "C:\Poker\hooter.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\hooter.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "David O'Keefe" Then
Open "C:\Poker\dokeefe.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\dokeefe.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Dave Martin" Then
Open "C:\Poker\davemartin.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\davemartin.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

ElseIf Player(PlayX) = "Mike" Then
Open "C:\Poker\mike.pok" For Input As #1
Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1
TotalGamesPlayed = TotalGamesPlayed + 1
gamebalance = gamebalance + playmoney
Open "C:\Poker\mike.pok" For Output As #1
Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
Close #1

Next PlayX
End If

Recommended Answers

All 6 Replies

Not knowing which version of vb you are using I do use vb6 for this.
Your loop is ok but you are not accessing the combos. You either have created and array of these or added these one by one. If the last is the case you need to set the index property to 1 to 10.
Here is a bit of code that demonstrates this:

Private Sub Form_Load()
Dim I As Integer
I = Player1(1).Index
MsgBox (I)
If Player1(1).Index = 1 Then
Player1(1).Text = "Hello"
End If
End Sub

Obviously you would have to replace my (1) with your (PlayX) of sort

If we reformat the code (with indentation) we get

Dim (PlayX) As Integer
For PlayX = 1 To 10

    If Player(PlayX) = "YT" Then
        Open "C:\Poker\yt.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\yt.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image3.Picture = Image4

    ElseIf Player(PlayX) = "Morbid" Then
        Open "C:\Poker\morbid.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\morbid.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image5.Picture = Image6

    ElseIf Player(PlayX) = "Big Head" Then
        Open "C:\Poker\bighead.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\bighead.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image20.Picture = Image19

    ElseIf Player(PlayX) = "Woody" Then
        Open "C:\Poker\woody.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\woody.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image7.Picture = Image8

    ElseIf Player(PlayX) = "Dog" Then
        Open "C:\Poker\dog.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\dog.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Darryl" Then
        Open "C:\Poker\darryl.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\darryl.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image23.Picture = Image22

    ElseIf Player(PlayX) = "E Honda" Then
        Open "C:\Poker\ehonda.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\ehonda.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Bandit" Then
        Open "C:\Poker\bandit.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\bandit.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image14.Picture = Image13

    ElseIf Player(PlayX) = "Dyksey" Then
        Open "C:\Poker\dyksey.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\dyksey.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Lucock" Then
        Open "C:\Poker\lucock.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\lucock.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image10.Picture = Image9

    ElseIf Player(PlayX) = "Stonch" Then
        Open "C:\Poker\stonch.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\stonch.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Jason N" Then
        Open "C:\Poker\jasonn.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\jasonn.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image34.Picture = Image35

    ElseIf Player(PlayX) = "Jonsey" Then
        Open "C:\Poker\jonsey.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\jonsey.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        Image26.Picture = Image24

    ElseIf Player(PlayX) = "John D" Then
        Open "C:\Poker\johnd.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\johnd.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Steve M" Then
        Open "C:\Poker\stevem.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\stevem.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Hooter" Then
        Open "C:\Poker\hooter.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\hooter.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "David O'Keefe" Then
        Open "C:\Poker\dokeefe.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\dokeefe.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Dave Martin" Then
        Open "C:\Poker\davemartin.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\davemartin.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    ElseIf Player(PlayX) = "Mike" Then
        Open "C:\Poker\mike.pok" For Input As #1
        Input #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1
        TotalGamesPlayed = TotalGamesPlayed + 1
        gamebalance = gamebalance + playmoney
        Open "C:\Poker\mike.pok" For Output As #1
        Write #1, PlayerName, TournamentGames, TotalGamesPlayed, Points, Member, gamebalance
        Close #1

    Next PlayX
End If

And take out the extraneous bits you see mismatched For-Next If-End If

For ...
    If ...
    Next ...
End If

Swap the last two lines and see how it works then.

I'm afraid I don't fully understand either answer.

I have 10 combo boxes and I had some repeated code shown above. Instead of Player(PlayX) it would go through Player1, Player2, Player3 etc

What I need is the word Player and the number after needs to incrememnt from 1 to 10.

I thought I could join the word Player with a variable (PlayX) but there is a error in the way I have done this.

I found a slight way round the problem and it works!

I added:

Dim PlayVar As String
PlayVar = "Player"

For PlayX = 1 To 10

PlayVar = PlayVar & PlayX

Now the For loop takes the word Player and add's the variable incrementing.

A strange way out of the problem. Would still appreciate any other suggestions.

Oh dear, after some testing I have found that it doesn't work. It seems to skip these lines.

To get it functioning I have added the hundreds and hundreds for old lines of code but still looking for a consolidating solution.

O.K lets start again VB6

Private Sub Form_Load()
Player(0) = "PLayer1"
Player(1) = "PLayer2"
Player(2) = "PLayer3"
End Sub

Private Sub Player_Change(Index As Integer)
Call chekIT
End Sub

Private Sub chekIT()
Dim I As Integer
Dim PlayX As Integer
Dim num As Integer
num = 1
For PlayX = 0 To 2

    If Player(PlayX) = "YT" Then
        'Put other code bits in here
        MsgBox ("YT")
        Player(PlayX).AddItem ("Games Played" & " " & num)
    End If

    If Player(PlayX) = "Morbid" Then
        MsgBox (Player(PlayX))
    End If

    If Player(PlayX) = "Big Head" Then
        MsgBox (Player(PlayX))
    End If
Next
End Sub

.I have created an array of three comboboxes, indexes 0,1,2. I have loaded player1,2,3 in the form load event. I have created a sub checkIT that does the work and will be called each time the text in one of the comboboxes changes. There need to be error code added to not load stuff more than once into the comboboxes!
Now I can't see how you assign your strings to form text that you save or variables declared to read your text back in.
However, try this code and modify it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.