Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

How to fix run time error 9 subscript out of range ?

Status
Not open for further replies.

davidck88

Programmer
Jan 5, 2009
27
NL
Hi all . I try to use the following code to load playlist text file into listboxes . But unfortuenlty i get this error (but the listboxes get loaded with mp3 path and title):

Run-time error 9

subscript out of range

pointing at the following line:

'Check for unwanted vbCrLf (which would yeild a "")
If Len(strArr(intLB)) <> 0 Then

could you guys help me fix this error so i be able to load playlist data in to listboxes without any error? My project has 2 listbox and one load button.

Looking forward for replies.Thanks


Code:
Private Sub Command1_Click()
'If Label1.Caption = "Not Connected" Then
'MsgBox "You must Connect to Room", vbCritical, "Not Connected"
'Else
Dim i As Long
Dim intLB As Long   'Is an index counter
Dim lngCtr As Long  'Represents the count of individual lines of data
Dim strOpenPathFile As String, strArr() As String, strBuffArr() As String
        
    With CommonDialog1
        .CancelError = True
        On Error GoTo CommDiaCancelled
        .Filter = "Text Files (*.txt)|*.txt|Word Doc (*.doc)|*.doc"
        .ShowOpen
    End With
    
    strOpenPathFile = CommonDialog1.FileName

    Open strOpenPathFile For Input As #1
        strArr = Split(Input(LOF(1), 1), vbCrLf)
    Close #1

    'Clear all the ListBoxs
    List1.Clear
    List2.Clear
     lngCtr = UBound(strArr)

    For intLB = 0 To lngCtr
    
CommDiaCancelled:
    If Err.Number = 32755 Then
        MsgBox "Cancel selected !"
        Exit Sub
    End If
        'Check for unwanted vbCrLf (which would yeild a "")
        If Len(strArr(intLB)) <> 0 Then

            'Split the Line of data
            strBuffArr = Split(strArr(intLB), vbTab)

            'Load the data into the ListBoxs

            List1.AddItem strBuffArr(0)
            List2.AddItem strBuffArr(1)
           
        End If
    Next
    Call Number(List1, " - ")
List1.Selected(0) = True
For i = List1.ListCount - 1 To 0 Step -1 ' counts list entries
    If List1.Selected(i) = True Then ' if selected then
        Text1.Text = List1.List(i) ' add to other list
        Text2.Text = List2.List(i)
        Text3.Text = List1.List(i)
        Text3.Text = Replace(Text3.Text, ".mp3", "")
        Text3.Text = Replace(Text3.Text, ".avi", "")
        Text3.Text = Replace(Text3.Text, ".asf", "")
        Text3.Text = Replace(Text3.Text, ".mpeg", "")
        Text3.Text = Replace(Text3.Text, ".mpg", "")
        Text3.Text = Replace(Text3.Text, ".wav", "")
        Text3.Text = Replace(Text3.Text, ".wmv", "")
        Text3.Text = Replace(Text3.Text, ".wma", "")
        Text3.Text = Replace(Text3.Text, ".cda", "")
        Text3.Text = Replace(Text3.Text, ".mid", "")
        Text3.Text = Replace(Text3.Text, ".midi", "")
        Text3.Text = Replace(Text3.Text, ".rm", "")
        RichTextBox3.TextRTF = Replace(RichTextBox1.TextRTF, "%song", Text3)
        If Option2.Value = True Then
        Else
        'Call RoomSend(RichTextBox3.TextRTF)
        End If
    End If
Next
'If Option1.Value = True Then
'wmp.URL = Text2.Text
'Timer1.Enabled = True
'Else
'MsgBox "You must press play then click on start buttom for the program to start", vbInformation, "Autoplay Off"
'End If
'End If
'Text10.Text = List1.ListCount
End Sub
 
Allow me to simplify the actual problem for you:
Code:
[blue]Option Explicit

Public Sub doit()
    Dim lp As Long
    Dim a(3) As String 'doesn't really matter
    
    
    On Error GoTo WildAndCrazyErrorHandling
    
    For lp = 0 To UBound(a)
    
WildAndCrazyErrorHandling:
        
        Debug.Print a(lp) ' error will appear to be here
    Next
    Debug.Print 0 / 0 ' this generates an error
    
End Sub[/blue]

 
Strongm thanks for your reply. i know the problem is some where in this block of code but don't know how to fix it ;
Code:
        'Check for unwanted vbCrLf (which would yeild a "")
        If Len(strArr(intLB)) <> 0 Then

            'Split the Line of data
            strBuffArr = Split(strArr(intLB), vbTab)

            'Load the data into the ListBoxs

            List1.AddItem strBuffArr(0)
            List2.AddItem strBuffArr(1)
           
        End If

i put massage box for intLB and found its value to be 7 but actual number of songs is 5! do you think the song path and song title saved on text were too big to fit in one line for each song ? why intLb value is not 4 at the end ? I think probably the problem is the way songs were numbred on run time!! I be happy if you tell me how to fix it.Thanks
 
The problem is..... your strArr(intLB) is returning a string without a tab character. Later, when you split the array based on the tab character, you are only getting one element, so the List1.AddItem is ok, but not the next line because there isn't a 1 element in the array, only a Zero'th element.

I would suggest that you check the string for the tab character before splitting it.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I'm afraid you really did not pay attention to what StrongM was trying to point out.

Your big problem here is that you are assuming that the only error that could possibly happen is the one you are expecting (32755). So when some other error happens you are doing nothing to catch it or handle it.

In essence, your error handler (for everything besides 32755) is on error-ignore-problem-and-go-to-here:

Code:
If Len(strArr(intLB)) <> 0 Then

At that point your code has skipped past certain things that should have gotten done, like initializing the array, or setting intLB, or whatever. And the worst part is that when you finally do get an error message, it will likely be completely unrelated to what the real error was in the first place.

If you are going to use error trapping as a way to check conditions (and I advise avoiding that if there is an alternative), the proper structure should be something like:

Code:
On Error Goto RegularErrHandler
'< do some stuff >
'< get to part that uses error trapping to detect condition >
On Error Resume Next
Err.Clear
'< do stuff that may raise the error you are expecting - let's say it is # 32755 >
If Err.Number = 32755 Then
  < Do stuff that should happen when this error occurs >
ElseIf Err.Number <> 0 Then
  < This is an error you were not expecting, get your regular err handler to deal with it>
  Goto RegularErrHandler
End If

'Turn regular error handling back on
On Error Goto RegularErrHandler
'< Do the rest of your stuff>

Exit Sub

RegularErrHandler:
  MsgBox "Error # " & Err.Number & " occurred: " & Err.Description
 
Thanks all i got it working now . I had some textboxes/richtextbox missing in my form !! so i changed the last part of code to this and now working well :

Code:
........

    'RichTextBox3.TextRTF = Replace(RichTextBox1.TextRTF, "%song", Text3)
        'If Option2.Value = True Then
        'Else
     
        'End If
    End If
Next

'Text10.Text = List1.ListCount
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top