VB6 UDT to text output

I am using an array of UDT to build a record, I also have a class that will write textfiles (not using put) I cannot seem to pass the UDT in, what am I missing?
Type PatientInfo
    ci As ClaimInfo 'another udt
    ID As String * 20
    AccountNo As String * 15
    FromDate As String * 10
    ThruDate As String * 10
End Type
Dim pi as patientinfo
after i fill this type in i want to write it out as simply as possible, I will actually create an array of the type then I will want to dump it to a text file.
LVL 7
cmrobertsonAsked:
Who is Participating?
 
inthedarkConnect With a Mentor Commented:
You have 3 choices how to dump your UDT record into a file.

1) A hard way using CopyMemory to copy the data from the address of PI for Len(PI) characters into a string that you already filled with blank spaces.

2) An easy way using Put.  You can use PUT to create text files.

' File Open

Dim lFile as long
lfile=FreeFile

Open "C:\MyFile.txt" for Binary Access Read Write Shared As #lFile


Dim lPos as Long

' Write Data to File loop through PI array

For lC = 0 To Ubound(PILIst)


    lPos = LOF(lFile) ' Get the current length of the file

    Put lFile, lPos, PIList(LC)

Next

Close lFile

3) Another way to use Put is to use fixed length records.

lFile = FreeFile
Open filename For Random Access Write As #lFile Len = Len(PI)

You need to keep a count of records in the file or use Lof to calculate records.

lNextRecord =1+ ( Lof(lFile)/Len(PI))

Put lFile, lNextRecord, PI


You can also read the data in the same way:

Get lFile, RecordNeeded, PI

TIP for using UDT files:

When you define your UDT give it a version number.

' new record
Type PatientInfoVNew
    ci As ClaimInfo 'another udt
    ID As String * 20
    AccountNo As String * 15
    FromDate As String * 10
    ThruDate As String * 10
End Type

' old record
Type PatientInfoVOld
    ci As ClaimInfo 'another udt
    ID As String * 20
    AccountNo As String * 15
    FromDate As String * 10
    ThruDate As String * 10
End Type

Because one day you will want to add new fields into the UDT when you do you wile have to write some code that copies the data to a new file in the new format data from VNew to VOld




0
 
OrcbighterCommented:
can you show the code where you pass in the udf?
0
 
aikimarkCommented:
A very old memory of VB had to do with the limitations in passing UDTs as routine parameter types.

What does your method definition look like?
What does your method invocation statement look like?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
cmrobertsonAuthor Commented:
the problem with my parameter in passing is it does not use put it uses print which will not allow the udt as its type. I was hoping for something in the way of conversion to string to flatten it out. Otherwise I will use the put method as described above. SO the passing isn't really my issue I want strvar = udtvar with whatever type converting would be needed. I suspect I cannot do it but looking to see if anyone has an idea
0
 
inthedarkCommented:
As I said in item 1 it can be done but it is very hard.

Put will do what you want with the need to use copymemory.
0
 
inthedarkCommented:
Sorry for poor English:

Put will do what you want without  the need to use copymemory.
0
 
VBClassicGuyCommented:
I have been using the put and get method to write typed variables out to a binary file for years and it works great and is fast. Use inthedark's answer.

To find out how many records are in the file, I do a LOF and divide it by the record length. To add a new record, I again use LOF, and add the new record to LOF/RecordLength + 1.

Be sure to open the fie as:

Open filename For Random Access Write As #lFile Len = Len(PI)

For examples, go here and scroll down about 3/4 of the page:

http://trixar.com/~makai/minidb1.htm

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.