Solved

Changing User Defined Data Types over Time...

Posted on 2004-04-27
3
271 Views
Last Modified: 2010-05-02
I have a question about UDTs that I can't find the answer to.... We have a UDT that we fill with data and then when program closes we save that UDT record to a file to reload it when program opens next time....

This works fine... My question is... Are UDT's accessed by the running code via the variable length declarations or by variable name...  If we use a record format for a year and then INSERT a new variable into the UDT will that mess up the reading of old records or does VB automagically read in the UDT based on the variable name declarations?  I suspect that it may mess up the saved UDTs if we add new variables into the UDT unless it is at the end of the UDT declaration????

Example:
Type myRecord
      myVar1(32) As Single
      myVar2(32) As Single
      myVar3(32) As Single
      myVar4(32) As Single
End Type

Now we save myRecord to a file as a binary data structure...  if we change the UDT by adding/inserting a new variable NOT AT THE END of the UDT will that mess up the reading of the records...
Type myRecord
      myVar1(32) As Single
      myVar2(32) As Single
      myVar2b(32) As Single  ' new var inserted
      myVar3(32) As Single
      myVar4(32) As Single
End Type

i.e. if we read in the above will it put the old myVar3 value into the new myVar2b value????

Also, if we declare the UDT above but at runtime need more space in the myVar1 array, can you use ReDim on a UDT variable???  and if so will it read in correctly next time... the reading of the data structures is done by code similar to:

   Open ClientFile For Random As 1 Len = Len(myRecord)
    Get #1, 1, myRecord
      On Error GoTo myFileReadErrorHandler
    Close

The saving of the UDT is done using something like:

    Open ClientFile For Random As 1 Len = Len(myRecord)
    Put #1, 1, myRecord
    Close #1

0
Comment
Question by:torrid333
[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
3 Comments
 
LVL 11

Assisted Solution

by:jmwheeler
jmwheeler earned 250 total points
ID: 10928561
If you are using UDT's to read/write to a file then you can not change them at all without causing a problem.  Adding a variable anywhere in the UDT (even at the end) will cause your code to read in more bytes then each of the old records actually takes up.  Therefore the bytes at the end of the record you read in will actually be the beginning bytes of the next record.  If you want to store data in a format in which you can add variables at any time I would use a database to store your data instead of files.  
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 250 total points
ID: 10929182
Can you check before you load?


ie

Type myOldRecord
     myVar1(32) As Single
     myVar2(32) As Single
     myVar3(32) As Single
     myVar4(32) As Single
End Type

Type myRecord
     myVar1(32) As Single
     myVar2(32) As Single
     myVar2b(32) As Single  ' new var inserted
     myVar3(32) As Single
     myVar4(32) As Single
End Type


If (FileLen(ClientFile) <> Len(myRecord)) Then     ' old format file
Open ClientFile For Random As 1 Len = Len(myOldRecord)
   Get #1, 1, myOldRecord
     On Error GoTo myFileReadErrorHandler
    Close
CONVERT_TO_NEW_RECORD_NOW
else
Open ClientFile For Random As 1 Len = Len(myRecord)
   Get #1, 1, myRecord
     On Error GoTo myFileReadErrorHandler
    Close

end if
0
 

Author Comment

by:torrid333
ID: 10932925
jmwheeler: your insight was helpful. Thank you.

EDDYKT: although a pain because the code gets real long this solution was implemented to help reduce errors loading data saved under the OLD record types...

I'm awarding points to both of you. Thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

752 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