Solved

reading text file iformation into list

Posted on 2001-08-14
5
148 Views
Last Modified: 2010-05-02
Hi can anyone give a hint as to how to read data from a text file and put it into a list ..  I can do the later but the reading and moving throught the text file has me..  Thanks in advance..  Derek
0
Comment
Question by:Delboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Accepted Solution

by:
JonFish85 earned 50 total points
ID: 6384542
try this:

Dim strTemp as string
Open "C:\MyFile.txt" For Input As #1
  Do Until EOF(1)
    Line Input #1, strTemp 'or without the "Line"
    List1.AddItem strTemp
  Loop
Close #1

hope this helps!
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6384575
or you could use something like this:

Option Explicit
'Change this to the # of characters
'you want to read in at one time
Const CHUNK_SIZE As Integer = 10

Private Sub Command1_Click()
Dim strTemp As String
Dim cnt As Long

  Open "C:\mytest.txt" For Binary As #1
    cnt = 1
    Do Until cnt >= LOF(1)
      strTemp = String(CHUNK_SIZE, 0)
      Get #1, cnt, strTemp
      List1.AddItem strTemp
      cnt = cnt + CHUNK_SIZE
    Loop
  Close #1
End Sub
0
 

Author Comment

by:Delboy
ID: 6384609
Great stuff..  ta   D
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6384618
glad I could help!
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6384622
Assuming that your file is not huge, and that you have one item per line in the file, the following would put the data into an array:

Dim aryList() As String  'Array/List of data
Dim hFile     As Integer 'File Handle
Dim Buffer    As String  'File Buffer

hFile = FreeFile()
Open "C:\MyFile.txt" FOR Binary Input As #hFile
Buffer = Input(LOF(hFile),hFile) ' Read in entire file
Close #hFile
aryList = Split(Buffer, vbCrLf) ' create the list of items


Now, aryList(0) will contain the first item, aryList(1) will contain the second item, and so on.

Study this code, and if you have any questions as to how it works, let us know.

FYI, the Split function is new in VB6. If you're using a prior version of VB, then we would have to write a comparable Split function, or go about it a slightly different way.


-Dennis Borg
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question