×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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

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

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.

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

(OP)
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,  

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

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources