Solved

VB6 UDT to text output

Posted on 2010-11-24
8
804 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

688 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