Results 1 to 12 of 12

Thread: Putting text file entries into an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Posts
    18

    Putting text file entries into an array

    Hey all!

    OK, what I want to do is put a whole bunch of names into an array from a text file. The names are already in correct order, all I want to do is get entry #1 into label #1, entry #2 into label #2 etc. How do I do this?

    This is what I've got, but I know it doesn't work.

    Code:
    Private Sub Form_Load()
    Dim Name as String, i as String
    
    Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
        For i = 1 To 52
            Input #1, Name
            lblPlayer(i).Caption = Name
            Name = ""
        Next i
    
    End Sub
    Thanks in advance for any help you can offer.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    How is the text file set up? Comma delimited, each player has a line?

  3. #3
    AIS_DK
    Guest
    Try:

    Code:
    Private Sub Form_Load()
    Dim Name as String
    dim i as byte
    
    Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
        For i = 1 To 52
            Input #1, Name
            lblPlayer(i).Caption = Name
        Next i
    close #1
    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Posts
    18
    Only player's names are in the text file, and there is one per line, like this:

    Leon Davis
    Damien Adkins
    Mark Richardson


    etc.

  5. #5
    AIS_DK
    Guest
    This was what I meant to post:

    Code:
    Private Sub Form_Load()
    Dim Name as String
    dim i as byte
    
    Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
        For i = 1 To 52
            Line Input #1, Name
            lblPlayer(i).Caption = Name
        Next i
    close #1
    End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Posts
    18
    No good... 'Run Time Error 54, Bad File Mode'

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Posts
    18
    No good... 'Run Time Error 54, Bad File Mode'

    And, it is completely clearing all text from the Players.txt file every time.

  8. #8
    AIS_DK
    Guest
    Sorry I hadn't seen that, you should be using

    Open "c:\lenny\collingwood\files\Players.txt" For Input As #1

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Posts
    18
    No, I need the text file to stay the same, and the program to read from the text file and put the text file information into an array.

    Therefore, I know that this is right:
    Open "c:\lenny\collingwood\files\Players.txt" For Output As #1

    But, what should I use here:
    For i = 1 To 52
    Line Input #1, Name
    lblPlayer(i).Caption = Name
    Next i

    instead of Line Input, in order to READ the file, not WRITE to it?

  10. #10
    AIS_DK
    Guest
    Well thats why you need to use "INPUT" instead of "OUTPUT"

    INPUT allows you to read from a file, while OUTPUT allows you to write to the file.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Posts
    18
    Oh... okay then. I was certain it was the other way around. Thanks

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    The best way, in my humble opinion, to load an entire file, line by line, into an array :

    VB Code:
    1. Dim myArray() As String
    2.     Open "c:\autoexec.bat" For Binary As #1
    3.         myArray = Split(Input(LOF(1), 1), vbCrLf)
    4.     Close #1
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width