Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

File Handling with UDT's

I am working on a VB app that has a very small volume of records.  I have decided to do it using the file handling functions built into VB.  Although the volume of records is small the number of fields in each record can be 20 or more.  It would be so much more convenient and flexible to be able to write a record to a file using a UDT rather than coding each field individually on the outfile.WriteLine statement.  I have been told that this is not possible on sequential files.  You can only write the UDT on Random files.  It was further explained that random files can be a pain when deleting records.  Although I am a nub at the file handling I do know VB.

The question: Can I write a sequential file using a UDT.  If so please provide examplesof writing these files and reading them back in.  If I can't use a UDT what would be the recommended techniuqe for writing and reading.  Do I need to add a vbCrlf at the end of the UDT?

Example UDT:
Public Type TestUDT
  Code As String * 3
  Name As String * 50
  t1 As String * 3
  t2 As String * 3
  t3 As String * 3
  t4 As String * 3
  t5 As String * 3
  t6 As String * 3
  t7 As String * 3
  t8 As String * 3
  t9 As String * 3
  t10 As String * 3
  t11 As String * 3
  t12 As String * 3
  t13 As String * 3
  t14 As String * 3
  t15 As String * 3
End Type

Thanks
0
mlcktmguy
Asked:
mlcktmguy
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No...you can't write UDT out to a sequential file.  You must open the file in random access mode.

If you don't want all of your records to run together in the file then add an extra string field of length two to your UDT and set its value to vbCrLF for each new record you create before writing it to the file.

Here is a small example of reading and writing a random access file:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20884901.html

Regards,

Idle_Mind
0
 
GeneMCommented:
You are correct in that the normal sequental I/O operations will not accept a UDT.  They expect a string.  A UDT is a container.

So the problem you have is how to convert a UDT into a string or a string into a UDT.  You can use the CopyMemory API to do that.  For example:

'The following UDT is somewhere in a module
Public Type DetailRec
    Name        As String * 26
    RecType     As String * 2
    Space1      As String * 1
    DID         As String * 4
    Min4Dec     As String * 7  
    Space4      As String * 1
    MinSecRnd   As String * 5  
End Type


'Then in my form, I may have something like this:
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSrc As Any, ByVal dwSize&)

Private Sub btnProcess_Click()
    Dim detdata As DetailRec
    Dim LineBuffer as String

'I now read in a record and move it to detdata
    Line Input #1, LineBuffer
    CopyMemory detdata, ByVal LineBuffer, Len(detdata)
'I can now reference detdata.Name etc.

'When you are ready to write back to the file, you must convert the UDT
'back to a string.
    LineBuffer = Space$(len(detdata))      
    CopyMemory ByVal LineBuffer, detdata, Len(detdata)
    Print #1, LineBuffer
 
I think the above will work for you.  Give it a try and let me know if you still have a problem.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Nice trick GeneM.  I'll have to remember that one...

Idle_Mind
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now