Solved

reading text file iformation into list

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now