Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the purpose of Record Types?

Status
Not open for further replies.

CybOrg

Programmer
Nov 30, 1998
57
SE
An old mainframe programmer, I'm used to the concept of records and record layouts when processing sequential files. Why can't I define a record type in VB like:

Type MyRecordLayout
RecordID as String * 6
FirstName as String * 20
etc, etc
End Type

Dim InputRcd as MyRecordLayout

Line Input #1 InputRcd

What I mean is of course I can DECLARE the record type, but whenever I try to stuff the read record into InputRcd I get a data type incompatibility error. The purpose of the construct must be another... What?

/Cy


 
Well, the reason you're getting the error is that Line Input expects a String or Variant type variable, not a user defined type. If you want to do your file reading like this, you'd have to write a wrapper function for Line Input.

VB doesn't deal much with data stored in sequential files. The focus is more on relational databases, and there are plenty of facilities for dealing with those.
The purpose of user defined types in VB is data grouping and organization. They're a very basic form of object, like a light-weight class. They're useful in circumstances where a full-blown class is more trouble than it's worth, like when all you want to do is store multiple values in one object with no methods or data protection needed. It's basically the same idea as a record layout, but it's designed to be general purpose rather than specifically for input.

I hope that made some sense.
 
Thanx! Your explanation makes sense. VB's poor handling of sequential files doesn't. Oh well - someday I guess we'll all create relational databases for even the simplest little sequential process... >:-<
 
Hi CybOrg,

I work with sequential files on a daily basis. I use the user defined type as a record layout for the input file. If you need an example of how I read in the records and export them as a fixed length records I would be glad to post it.

Swi
 
CybOrg -

You can read in from your file into a string, and then call an api variously named &quot;CopyMemory&quot;, &quot;RtlMoveMemory&quot;, etc. to copy the contents of the string into the memory used by your record. You need this declare statement:

Code:
Public Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; (Destination As Any, Source As Any, ByVal Length As Long)

And you'll use it like:
Code:
Dim MyRec as MyRecordLayout
Dim sTemp as String * 231;

   Call ReadMyRecordFromFlatFile(sTemp)
   CopyMemory(MyRec, ByVal VarPtr(sTemp), 231)
   ' MyRec now has the data in it.

Note that the string was declared of fixed size. It must match the length of your recordlayout precisely. The reason is that VB normally allocates memory for strings dynamically, and if you copy beyond the end of the string, you'll get an access violation. By using a fixed-length string, VB pre-allocates exactly that much memory for you.

You can also get around it by using dynamic strings by making sure the string contains enough data before the copy:
Code:
   Dim sTemp as string

   Call ReadMyRecordFromFlatFile(sTemp)
   If Len(sTemp) < 231 Then
      sTemp = sTemp & Space$(231 - Len(sTemp))
   End If
   CopyMemory(MyRec, ByVal VarPtr(sTemp), 231)

Chip H.
Error on line 9: Object of type SIGNATURE expected
 
Or, of course, you can open the file for Random access, and use Get - which DOES allow UDTs...
 
Thanks, guys! Lots of goodies to try!
/Cy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top