472,110 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,110 developers and data experts.

How to read a file in VB - Part 1 - VB6, Line Input

8,435 Expert 8TB
Here is a very simple example routine which reads a text file, one line at a time. This uses the built-in VB statements only. Later we will cover the FileSystemObject, which provides greater functionality at the expense of slightly greater code complexity.

This self-contained routine can be pasted into a code module and called from anywhere, including the immediate window. It will expect you to pass the name of a file (including the path if it isn't in the current directory) and will copy the contents of the file to the immediate window. Note it will also avoid, as far as possible, interfering with any other processing which may be going on at the time.

Expand|Select|Wrap|Line Numbers
  1. Public Sub DumpFile_V01(ByVal FileName As String)
  2.   Dim FileNo As Long
  3.   Dim LineNo As Long
  4.   Dim LineText As String
  5.  
  6.   FileNo = FreeFile ' Get next available file number.
  7.  
  8.   Open FileName For Input Access Read Shared As #FileNo
  9.   Do Until EOF(FileNo) ' Repeat until end of file...
  10.     Line Input #FileNo, LineText ' Read a line from the file.
  11.     LineNo = LineNo + 1
  12.     Debug.Print Format(LineNo, "00000"); ": "; LineText
  13.     DoEvents ' Allow Windows to handle other tasks.
  14.   Loop
  15.   Close #FileNo
  16. End Sub
May 16 '07 #1
0 54582

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by JrdA | last post: by
5 posts views Thread by Rafal 'Raf256' Maj | last post: by
18 posts views Thread by jas | last post: by
40 posts views Thread by Abby | last post: by
1 post views Thread by Magix | last post: by
2 posts views Thread by peter.vanna | last post: by
9 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
6 posts views Thread by arnuld | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.