Solved

reading text file iformation into list

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

765 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