Solved

reading text file iformation into list

Posted on 2001-08-14
5
145 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
  • 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

Technology Partners: 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 I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

679 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