Read from text file and store in array

I am trying to read from a text file and save the data in an array. This should be simple, but when I run it, I get the error- "Subscript out of range" or "Type mismatch."   What am I doing wrong?

x = 0
Open FileName For Input As #1
Do While Not EOF(1)
   Line Input #1, officeId(x)
   x = x + 1
Loop
Close #1
jennifereAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one way to do it:

Private Sub Form_Load()
    Dim fileName As String
    Dim officeid() As String
    Dim x As Integer
   
    fileName = "C:\SomeFile.txt"
    x = 0
    Open fileName For Input As #1
    Do While Not EOF(1)
        ReDim Preserve officeid(x + 1)
        Line Input #1, officeid(x)
        x = x + 1
    Loop
    Close #1
End Sub

Here is a different way to do it with Collections:

Private Sub Form_Load()
    Dim fileName As String
    Dim inputLines As Collection
    Dim inputLine As Variant
    Dim curLine As String
    Dim x As Integer
       
    ' read them in
    inputLines = New Collection
    fileName = "C:\SomeFile.txt"
    Open fileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, curLine
        inputLines.Add curLine
    Loop
    Close #1
   
    ' spit them back out
    For Each inputLine In inputLines
        Debug.Print inputLine
    Next inputLine
   
    ' spit them back out a different way (less effecient in collections)
    For x = 1 To inputLines.Count
        Debug.Print inputLines.Item(x)
    Next x
End Sub

Idle_Mind
0
EDDYKTCommented:
Or

Dim officeId
Dim ff As Integer, Filename As String, s As String
   
Filename = "File Name"
ff = FreeFile
Open Filename For Binary Access Read Lock Read Write As #ff
s = Space$(LOF(ff))
Get #ff, , s
Close ff

officeId = split(s, vbCrlf)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enariCommented:
The size of the array needs to be set

DIM OfficeID() as varient

Then the size needs to be declared.

REDIM OfficeID(15) creates an array of 15.

If you don't know how big it is, you do what Idle_Mind did, and

REDIM PRESERVE OfficeID(x+1)

Be sure to keep track of X so you know the size once you finish!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
EDDYKT's code is simple, elegant and will theoretically work with any size file.

From the help file:

"A variable-length string can contain up to approximately 2 billion (2^31) characters."

So as long as you have enough memory and your file doesn't have more than 2^31 characters, you are good to go. =)

The code I wrote using the ReDim Preserve is easy to read, but will be EXTREMELY slow for large files.

Idle_Mind
0
jennifereAuthor Commented:
Thanks - that works!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.