Search Tools Links Login

Using LSet to make reading files easier

Have you ever needed to read a fixed length text file in Visual Basic? I have written quite a lot of applications where I will receive a fixed length text file from the mainframe.
Tired of using the Left$, Right$ and Mid$ functions to parse each line for the individual elements of the line? Then read on.
Oh, and thanks to Rockford Lhotka's, book on Visual Basic 6 Business Objects for giving me this idea.

Original Author: Jerry Barnett


Using the LSET keyword you can use a user defined type (UDT)
to automatically parse a line from a text file into the
individual ONE LINE OF CODE! Here is how:

First lets take a sample file (call it 'SOMEFILE.TXT'):

08072000Jerry M Barnett 0002356A2S56D9

Ok, the line above represents lets say one of a few hundred
lines. The layout of the file is as follows:

Field Type Position Remarks
Date MMDDYYYY 1-8 Format will be MM/DD/YYYY
Name AlphaNumeric 9-29 Padded with space
Amount Numeric 30-36 9(5)v99
Code Alpha 37 A for accepted,
R for rejected
Account AlphaNumeric 38-43

First thing is to create a user defined type representing the
layout of the file. (Note - this should be place in the Module
level of a program.)

Type udtInput
MyDate As String * 8
Name As String * 21
Amount As String * 7
Code As String * 1
Account As String * 6
End Type

Note - All types are strings reguardless of
the type in the file. This will become clearer later. Next,
create a user defined type to represent the total length of the
line (I will explain why later)

Type udtLine
strBuff As String * 43
End Type

Now in your program you can do the following:

Sub Main()

' File Number

Dim iMyFile As Long

Dim sLine As udtLine

Dim sInput As udtInput

' Open your file for reading

Open App.Path & "SOMEFILE.TXT" for Input Access Read as #iMyFile

' Read the line into the udtLine UDT strBuff element

' This needs to be done, because you can't place a string

' directly into the UDT or you will get a type missmatch errorLine 

Input #iMyFile, sLine.strBuff

' The buffer (strBuff) represents the entire line

' Now copy the udtLine UDT (sLine) into the udtInput

' UDT (sInput) *** ONE LINE OF CODE! ***

LSet sInput = sLine

' Wolla! You can now access each element of the sInput UDT!

Debug.Print sLine.Name

' Will print: Jerry M Barnett

' (with eight trailing spaces)

' Convert numeric String Amount value to a Long value with 2 decimal places

Debug.Print CDbl(Val(sLine.Amount)/100)

' Would print: 23.56

Debug.Print MyDateFunction(sLine.MyDate)

' Will print: 08072000 in any format you wish

' Note - The MyDateFunction is a function you define

' to parse the date string to the proper format you want.

End Sub

That's it! Hope you find this of

Note this can also be used (with
modification to' WRITE a flat file out.)

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 124 times


Visual Basic 6

Posted: 9/3/2020 3:45:00 PM
Size: 2,276 bytes

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.