Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File Handling with UDT's

Posted on 2004-08-09
3
Medium Priority
?
272 Views
Last Modified: 2010-05-02
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
Comment
Question by:mlcktmguy
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11757267
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
 
LVL 3

Accepted Solution

by:
GeneM earned 800 total points
ID: 11758940
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11778084
Nice trick GeneM.  I'll have to remember that one...

Idle_Mind
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

772 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