PDA

View Full Version : Runtime Error 9 : Subscript out of Range



InLaNoche
01-02-2015, 02:30 PM
I've been trying to wrap my head around this for an hour or so, but I still can seem to pin point why I am getting this error... Here is the code

Private Sub Form_Load()
Dim pArray() As String
Dim aTextbox As Access.TextBox
Dim aSub As Access.SubForm
Dim i As Integer
Dim downer As Integer

Me.ScoreRow.SetFocus
'check if score sheet has already been created (debugging testing made easier)
If DCount("*", "Hands", "[GameID] = " & Me.GameID) < 1 Then
'create the score sheet since it has not been done for this game

'set the gameID and starting hand number on the first new record
Me.ScoreRow.Controls("GameID").Value = Me.GameID
Me.ScoreRow.Controls("Hand").Value = 1

'need to generate 1 record row for each hand, and have the proper hand numbering
Me.ScoreRow.SetFocus
'after the turnaround (mid) hand, the hand count decreased (from i) by multiples of 2 - really!
downer = 2
For x = 2 To Me.totalhands
DoCmd.RunCommand acCmdRecordsGoToNew
Me.ScoreRow.Controls("GameID").Value = Me.GameID
If x > Int(Me.totalhands / 2) + 1 Then
'count down instead of up
Me.ScoreRow.Controls("Hand").Value = x - downer
downer = downer + 2
Else
'count up
Me.ScoreRow.Controls("Hand").Value = x
End If
'set the playerIDs for each line item
Debug.Print x
For i = 1 To Me.noplayers
'****ERROR MARKED ON NEXT LINE********************************
Me.ScoreRow.Controls("P" & CStr(i)).Value = pArray(i - 1)
Next
Next
DoCmd.RunCommand acCmdRecordsGoToFirst
Else
MsgBox "Scoreshee has been created previously. Do not add more rows..."
End If
'prevent more records from being created for this scoresheet
pArray = Split(Me.playerlist, ",")
'Get the player names, based on the IDs
For i = 1 To Me.noplayers
Set aTextbox = Me.Controls("Player" & CStr(i))
aTextbox.Visible = True
aTextbox.Value = DLookup("[PlayerName]", "Players", "[PlayerID] = " & pArray(i - 1))
Me.ScoreRow.Controls("S" & CStr(i)).Visible = True
Me.ScoreRow.Controls("B" & CStr(i)).Visible = True
Me.ScoreRow.Controls("M" & CStr(i)).Visible = True
Me.ScoreRow.Controls("P" & CStr(i)).Value = pArray(i - 1)
Next
Me![ScoreRow].Form.AllowAdditions = False
'if you poen a previous game, lock it so no changes are made
If Me.done Then
Call EndGame
End If
End Sub



offending line of code is :
Me.ScoreRow.Controls("P" & CStr(i)).Value = pArray(i - 1)

I have marked off above the line with a comment. I'm not sure why it is flagging this when I can set the same values (done near the bottom) for just the first record when I comment out the for line causing the error. Basically I am generating a score sheet (recordset based on the GameID), and need the player's ID on each row (for later record keeping and stats).

jonh
01-02-2015, 04:38 PM
pArray isn't filled yet.

InLaNoche
01-05-2015, 09:35 AM
crap.... Thanks for pointing that out. I was resorting the code, and left that part near the bottom.... :(