Subscript Out of Range Error 9 RRS feed

  • Question

  • Hi guys

    I need your help. I am trying to read the strings from the text files to get the array and split it, but I keep getting an compile error: Subscript out of range.

    Here's the code:

    Public Function AddToList() As String
    Dim i As Integer
    For i = 0 To File1.ListCount - 1
    Dim Test As String
    Open File1.Path & "\" & File1.List(i) For Input As #1
    Do Until EOF(1)
    Line Input #1, Test
    List1.AddItem Split(Test, ",")(1)
    Close #1
    End Function

    Code: Private Sub List1_Click() Dim str() As String
    Dim i As Integer
    str = Split(AddToList, ",")(1)

    For i = 0 To UBound(str)
    MsgBox (str(i))
    End Sub

    The error are highlighting on AddToList in this line:

    str = Split(AddToList, ",")(1)

    I don't know what they wanted me to do?

    Any advice would be much appreciate.
    • Moved by Liliane Teng Monday, November 1, 2010 8:11 AM Please repost on appropriate forum for better support (From:Visual Basic General)
    Friday, October 29, 2010 12:36 AM


All replies

  • You are declaring str as an array, and Split returns an array.  The line you are getting the error on, you are asking for item 1 of the array, not the array itself.  I suspect the fix you want is to not use the index 1 on the call to split, making the new code:
        str = split(AddToList, ",")

    Does the rest of your code really work in ..Net?  This looks more like VB 6...

    Friday, October 29, 2010 12:54 AM
  • is this a vb.net or a vb6 question?

    that code is pre vb.net.

    as far as the error goes, there oviously are less than 2 items when you split AddToList, which isn't surprising because the function doesn't return anything...


    if you show us an example of your text files, you might get a more useful answer

    Friday, October 29, 2010 1:00 AM
  • This is vb6. So if I am posting in the wrong forum then I would like to apology to you all. Over the top of this, I don't know what to do to fix the error I am getting?
    Friday, October 29, 2010 1:19 AM

  • So are you compiling this in vb6, or in vb.net?  If vb 6, see this post for guidance: http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b
    If you want this code to run in vb.net, let us know and it should be easy to update.

    • Marked as answer by Mark103 Sunday, January 2, 2011 7:16 PM
    Friday, October 29, 2010 1:30 AM