Solved

reading text file iformation into list

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
Vb.net 2008 2 56
I need help embedding an image as HTML in my vb.net application 3 49
MsgBox 4 47
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 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

13 Experts available now in Live!

Get 1:1 Help Now