Solved

VB6 UDT to text output

Posted on 2010-11-24
8
780 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:cmrobertson
8 Comments
 
LVL 9

Expert Comment

by:Orcbighter
ID: 34211994
can you show the code where you pass in the udf?
0
 
LVL 17

Accepted Solution

by:
inthedark earned 500 total points
ID: 34212563
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
 
LVL 45

Expert Comment

by:aikimark
ID: 34212714
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 7

Author Comment

by:cmrobertson
ID: 34213717
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
 
LVL 17

Expert Comment

by:inthedark
ID: 34213751
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
 
LVL 17

Expert Comment

by:inthedark
ID: 34213843
Sorry for poor English:

Put will do what you want without  the need to use copymemory.
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 34214804
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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now