reading a file

I have a file that I need to input and reformat.  I have declared public types for the layout of the file(ex: mypublictype).  the file is fixed length, not delimited.  Once I have declared the type, I open the file for input and get a record.  What I am presently doing is defining each field using mid statements.  What I would like to do is have a command that say Input #1, mypublictype
and this command will break the record into fields based on my type declaration.  What I have found is that I can't do this because my file is not comma delimited.  Is there any way around this?  I can submit code if this is confusing.
LJ083198Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vettrangerCommented:
The way you do this is to create a user defined type containing the layout of the fixed lenght fields in your fixed length record. I've included an example of the Type layout and sample code to access the file (this is modified slightly from the example code in the VB Get statement help.)

Type aRecord   ' Define user-defined type.
   ID As Integer
   Name As String * 20
   Zip as String * 9
End Type

Dim MyRecord As aRecord, Position   ' Declare variables.
' Open sample file for random access.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' Read the sample file using the Get statement.
Position = 3   ' Define record number.
Get #1, Position, MyRecord   ' Read third record.
Close #1   ' Close file.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ventondCommented:
Here is a sample that works:

Private Type mypublictype
    a As String * 4
    b As String * 6
    c As String * 10
    fill As String * 2
End Type
Private d As mypublictype
Private e As mypublictype

Private Sub Command1_Click()
    MsgBox d.a & vbCrLf & d.b & vbCrLf & d.c
    MsgBox e.a & vbCrLf & e.b & vbCrLf & e.c
   
End Sub

Private Sub Form_Load()
    Open "c:\vbapps\a.txt" For Binary As #1
    Get #1, Len(d) * 0 + 1, d
    Get #1, Len(e) * 1 + 1, e
    Close #1
End Sub

Form_Load read the forst two records of a fixed length file. Command1_Click displays them, you may want to loop until you have read all the records.

Note: in my Type I have a fill field because i seperated my records with a CRLF in the text file. Vettranger didn't. Vettranger: you beat me to the answer again!
0
vettrangerCommented:
ventond, the filler method in the case of a CRLF on the end of the record is a good technique, I'm glad you included that.
0
ventondCommented:
Here is a sample that works:

Private Type mypublictype
    a As String * 4
    b As String * 6
    c As String * 10
    fill As String * 2
End Type
Private d As mypublictype
Private e As mypublictype

Private Sub Command1_Click()
    MsgBox d.a & vbCrLf & d.b & vbCrLf & d.c
    MsgBox e.a & vbCrLf & e.b & vbCrLf & e.c
   
End Sub

Private Sub Form_Load()
    Open "c:\vbapps\a.txt" For Binary As #1
    Get #1, Len(d) * 0 + 1, d
    Get #1, Len(e) * 1 + 1, e
    Close #1
End Sub

Form_Load read the forst two records of a fixed length file. Command1_Click displays them, you may want to loop until you have read all the records.

Note: in my Type I have a fill field because i seperated my records with a CRLF in the text file. Vettranger didn't. Vettranger: you beat me to the answer again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.