Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File Handling with UDT's

Posted on 2004-08-09
3
Medium Priority
?
271 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
[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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

715 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