Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
10 Commandments 

In Memoriam

Terms of Use

VB Petition

Modules: Reading Text Files

Chuck Grimsby

The class below is what I've been using for the last year or so in both the VB and Access programs I've done, and combines most of the methods I've developed over the years. As with most things like this, they start out simple them become increasingly complex. Still, this one isn't too bad. Once it's in your project, the calls are easy, and the class is really quick!

Please note that this class only handles the reading of a file. What you do with the "line" once you've got it is not addressed by the class. (Usually, such things have to be handled in a method specific to the project.)

In addition to "standardizing" the handling of files, it also gives the programmer a "look ahead" and "look behind" feature that allows the programmer to "see" the next line before it's actually used, and the last line it worked with. This is =really= nice if you ever have to import/deal with "multi-line" or "label" type file formats.

Class keeps 3 lines for the programmer to use:
  .Text = current line to work with
  .PreviousLine = the last line.
  .NextLine = The next line for 'look ahead' operations
(occasionally useful to know what's next!)


2000-2002, Chuck Grimsby, All Rights Reserved.


Release Notes:

  1. The class has it's own EOF property (EndOfFile). Use that rather then the channel's EOF. Since the class is doing it's own handling of channel numbers, inputs, etc., using one that the class isn't handling could return erronious information.
  2. The BytesLeft should also be ignored. The class uses it internally for it's own purposes. The .LeftOver property is also something that the class uses for it's own use. Leave it alone!
  3. BufferSize can be set to whatever the programmer needs/wants. On files that have more then 4096 characters per line, this can be changed. Unless that's the case, 4096 bytes (the default) usually works quite well.
  4. The .StripLeadingSpaces and .StripTrailingSpaces properties are used for formatting of the line within your program. Use as needed/desired. 
  5. The .NoBlankLines property is for use when you'd rather not deal with blank lines. Note that this setting doesn't apply to the .NextLine propery or the .PreviousLine property.
  6. The .StripNulls becomes useful in files that are "padded". I use this a lot on files that come off of Main Frames and are really ment to be printed, rather then read into a program. Most files =don't= have nulls (Chr$(0)) in them, so normally you'll leave this off. (Turning it on slows the reads!)
  7. The .OnlyAlphaNumericCharacters property is also fairly useless except in some extremely rare conditions. I can't remember why I put this in here, it changes the class so that it only returns the characters in the ASCII range of 32 - 127 (Characters that can be printed), but not the so-called "upper-ASCII" characters that can also be printed. International users will probably find it =much= more useless then those in the US.
  8. The .CountOnlyNonBlankLines is for the count of lines. Personally, I almost always leave this set to False as I'd rather have a count that is more true to the format of the file, but I've had occurances where this is useful. 
  9. You can set the LineDelimiter property to be whatever you want/need. This becomes =really= helpful in files that are created on systems that don't use either Cr, Lf, or CrLf as line delimiters. The class will automatically try to figure out what the line Delimiter is if you don't set this. It looks for Carridge Returns (Chr$(13)), Line Feeds (Chr$(10)) and the 2 of them together. The class will automatically look for FormFeeds and use those as additional Line Delimiters.
  10. This class can also be used on FixedWidth files. Just set the .FixedWidthLineLength property to the length of the line, and that's it. When the .FixedWidthLineLength >0, the line delimiters are ignored, so feel free to do so in your code. 
  11. The methods used are:
      .cfOpen to open the file set in the .FileName property
      .csGetALine which is used to get the next line from the file
      .cfCloseFile to close the file when you're done with it.
    Watch the .EndOfFile property to know when you're done.

Contact Information:

Please contact Chuck Grimsby for any additional questions.

1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer