Solved

File Handling with UDT's

Posted on 2004-08-09
3
266 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 85

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 200 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 85

Expert Comment

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

Idle_Mind
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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