Solved

File Handling with UDT's

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now