?
Solved

reading a file

Posted on 1999-11-03
4
Medium Priority
?
144 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:LJ083198
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
vettranger earned 300 total points
ID: 2180190
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
 
LVL 2

Expert Comment

by:ventond
ID: 2180228
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
 
LVL 8

Expert Comment

by:vettranger
ID: 2180271
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
 
LVL 2

Expert Comment

by:ventond
ID: 2180344
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

592 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