Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Runtime error '9': Subscript out of range.

Status
Not open for further replies.

NewToVB

Computer
May 4, 2007
5
0
0
CA
Hi there,

I am new to VB and I was given a task to look at this little VB program which was written by other programmer. It was strange that I was able to open it and created an EXE on one machine before but now I keep getting this runtime error. Attach is the code.

Any help is appreciated

Private Sub LoadArrayPolls()
' Load the Document Types and related symbols into an array and then populate a combo box
Dim PollStr As String
Dim Indexcount As Integer ' Counter to load the ListBox
Dim Arraycount As Integer ' Counter to load the ListBox
Dim NumberPollFile As Integer ' File number assigned

Dim PollNo As Integer
Dim NumberofRaces As Integer ' Number of Races in this Polling Station
Dim RaceNo1 As Integer
Dim RaceNo2 As Integer
Dim RaceNo3 As Integer
Dim RaceNo4 As Integer
Dim RaceNo5 As Integer
'Dim RaceNo6 As Integer

' Retrieve a free file number
NumberPollFile = FreeFile

' Assemble the string for the Poll station information file which includes the subdirectory paths
PollStr = ELECTIONDRIVE & ELECTION2001DIR & DATADIR & CONFIGURATIONDIR & POLLSTATIONFILE

' Open the Polling Station text file containing the races to read into the Poll array
Open PollStr For Input Access Read Shared As #NumberPollFile

' Initialize Array Counter
Arraycount = 0

Do Until EOF(NumberPollFile)
' Read the Poll Number and the Races from the Poll file
Input #NumberPollFile, PollNo, NumberofRaces, RaceNo1, RaceNo2, RaceNo3, RaceNo4, RaceNo5

' Store the information into the Poll array
' Store Poll number in array
ArrayPolls(Arraycount, 0) = PollNo
' Store Number of Races for this Polling Station in array
ArrayPolls(Arraycount, 1) = NumberofRaces
' Store Race1 number in array
ArrayPolls(Arraycount, 2) = RaceNo1
' Store Race2 number in array
ArrayPolls(Arraycount, 3) = RaceNo2
' Store Race3 number in array
ArrayPolls(Arraycount, 4) = RaceNo3
' Store Race4 number in array
ArrayPolls(Arraycount, 5) = RaceNo4
' Store Race5 number in array
ArrayPolls(Arraycount, 6) = RaceNo5
' Store Race6 number in array
'ArrayPolls(Arraycount, 7) = RaceNo6

' Increment the counter
Arraycount = Arraycount + 1
Loop

' Close the file
Close #NumberPollFile

For Indexcount = 0 To (Arraycount - 1)
frmPoll_Station.lstPollNumber.AddItem ArrayPolls(Indexcount, 0)
Next Indexcount

If Arraycount > 0 Then
frmPoll_Station.lstPollNumber.Selected(0) = True
End If
End Sub
 
Replies continue below

Recommended for you

Which line is erroring out? That error means that you've tried to access an element of an array, list box, etc. that's outside its limits. For example, trying to access element -1, or element 6 of a 5 member array.
 
ArrayPolls(Arraycount, 0) = PollNo. This line gives me error.

I declared the Const NUMOFPOLLS As Integer = 206 to read the ArrayPolls
Dim ArrayPolls(NUMOFPOLLS, NUMOFRACEINFOCOL) As String.

I may have to look into the text file for the array.

Thanks,

 
Sorry, did not have a chance to reply until today. I found out what the problem was. I changed the path to point to C:\

Thanks for the help guys.
 
Status
Not open for further replies.
Back
Top