Results 1 to 17 of 17

Thread: [RESOLVED] Putting mixed data tyes to random access files

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Resolved [RESOLVED] Putting mixed data tyes to random access files

    I am familiar with using random access files, which I used extensively in the DOS (!) QBasic days. In Vb6 I use user defined types to lay out the bytes on disk to be allocated to various strings as in
    Code:
    Private Type DataBlock
    Busname as String * 30
    Tel As String *10
    ...
    ...
    End Type
    I'm quite comfortable storing strings to a random access file. My question is, if I wish to store say an Integer (2 Bytes) or a Long (4) on disk how do I convert it.
    In QB it used to be MKI$ to write (put) and CVI to read (get). For the next up (Single) it used to be MKS$ to write and CVS to read. They were different as they took up 2 or 4 Bytes on disk, as applicable.
    What is the syntax in VB6 for Byte, Boolean, Integer , Long and Single. (it can't be CStr as the Byte sizes on disk would be ambiguous and if the numeric data was scattered in the UDT type structure the offsets would be scrambled) to there must be a different syntax for each numeric type..
    Thanks all !

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Putting mixed data tyes to random access files

    Just add them your type.
    Using Put and Get of the type using binary File IO

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Putting mixed data tyes to random access files

    Sample:
    Code:
    Option Explicit
    
    Private Type tpVBForum
      sSubForum As String
      lNofPosts As Long
      lNofViewing As Long
    End Type
    
    
    Private Sub Command1_Click()
      Dim sFile As String
      Dim fID As Integer, i As Long
      Dim tVBForum() As tpVBForum
      
      ReDim tVBForum(1)
      tVBForum(0).sSubForum = "Visual Basic 6 and Earlier"
      tVBForum(0).lNofPosts = 1622132
      tVBForum(0).lNofViewing = 323
      tVBForum(1).sSubForum = "Mobile Development"
      tVBForum(1).lNofPosts = 1569
      tVBForum(1).lNofViewing = 8
      
      
      sFile = "c:\temp\vbforum.dat"
      If Len(Dir$(sFile)) > 0 Then Kill sFile
      
      fID = FreeFile
      Open sFile For Binary As #fID
        Put #fID, , CLng(UBound(tVBForum) + 1)
        Put #fID, , tVBForum
      Close #fID
    End Sub
    
    Private Sub Command2_Click()
      Dim sFile As String
      Dim fID As Integer, lNofRecords As Long, i As Long
      Dim tVBForum() As tpVBForum
      
      sFile = "c:\temp\vbforum.dat"
      
      fID = FreeFile
      Open sFile For Binary As #fID
        Get #fID, , lNofRecords
        ReDim tVBForum(lNofRecords - 1)
        Get #fID, , tVBForum
      Close #fID
    
      For i = 0 To lNofRecords - 1
        Debug.Print tVBForum(i).sSubForum, tVBForum(i).lNofPosts, tVBForum(i).lNofViewing
      Next i
    End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Putting mixed data tyes to random access files

    Sorry Arnoutdv. Too long and complex to get my head round!
    Although I do know how to add them to the type structure. But there must be another step
    I'll simplify and rephrase the question. In QB to put a number to disk if it was say an Integer (2 Bytes long in the random file record structure) I would write say Bloggs$ = MKI$(number), and to read it I would write number = CVI(Boggs$).
    I know from experiment that to read an integer from a random access file in Vb6 I need CInt(), but what is the keyword for write an integer to disk and how does it differ from Single, Long, Boolean etc which take different space?
    Thanks all !

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Putting mixed data tyes to random access files

    It's just a simple Put and Get.
    These methods take care of the correct handling of variables types used in the type definition.
    There is absolutely no need for fiddling around with numbers to strings and vice versa.
    Code:
    Private Type DataBlock
      Busname as String * 30
      Tel As String *10
      SomeNumber As Integer
    End Type
    My code is just a basic example showing how to deal with an array of the UDT.
    Fill the array
    Write it disk
    Read it from disk
    Display the data

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Putting mixed data tyes to random access files

    Thanks for bearing with me. Do you mean that the Type declaration is enough? I found from experiment to need the conversion out (eg CInt) when I read. Mind you, that's when I read from old QB random access file. Perhaps I should experiment with a new Vb6 file?
    Last edited by el84; Oct 27th, 2022 at 02:52 AM.
    Thanks all !

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Putting mixed data tyes to random access files

    The type declaration is enough
    I have no experience with QB and/or it's random access files.
    I would definitely try with a new file based on the VB6 methods.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Putting mixed data tyes to random access files

    If you really have old data in files you need to bring forward to 1998 it might be safest to take the long road one time.

    Use a compatible version of QB to read the data and export it as comma or tab delimited text. The use a VB6 program or even a short VBScript to import the text into a Jet database. All done.

    There may have been conversion tools that shipped back around VB3 and earlier to deal with the old DOS data types. I doubt they are going to be easy to find 30 years later.

    I just can't see any upside to screwing around with Random files at this late date, or even way back in 1998 when VB6 came out.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: Putting mixed data tyes to random access files

    Use a Variant in your UDT!

    FYI: Variants take 16 bytes. And here's the complete list of the "types" of data they can store. However, in VB6, in the cases of Arrays, Objects, or Strings, they only store a pointer and not the actual data. In the cases of Byte, Integer, Long, Single, Double, Currency, LongLong, Date, & Decimal, they store the actual data inside the Variant. That's also true in the cases of storing a Null, Empty, and some of the other "indicator" values.
    Last edited by Elroy; Oct 27th, 2022 at 09:43 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: Putting mixed data tyes to random access files

    Now, if you really need to read some of that old floating-point data from old files, here's the link that shows you how to do that which was already posted for you.

    You don't need to worry about MKI/CVI, as Microsoft has stored an Integer (there was no Long) using 2s-complement basically forever. So those Integers read-and-write the same way they always did.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Putting mixed data tyes to random access files

    [QUOTE=dilettante;5584069]If you really have old data in files you need to bring forward to 1998 it might be safest to take the long road one time.

    ..../QUOTE]

    You've misunderstood my question. I have already successfully read all my QB random access files; that is not a problem. My question was in connection of how I should use variable on VB6 random access files in VB6.
    Thanks all !

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Putting mixed data tyes to random access files

    Quote Originally Posted by Elroy View Post
    Now, if you really need to read some of that old floating-point data from old files, here's the link that shows you how to do that which was already posted for you.
    As I explained in the previous post, my question was about using random access in future. I've dealt with all my previous apps.
    Thanks all !

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Putting mixed data tyes to random access files

    Using Random File Access

    Look in the Reference section for the Open, Get, and Put statements for more details.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Putting mixed data tyes to random access files

    Thanks all, and especially Arnoutdv. I have successfully tried all the data types, and all is well just with the type structure, as long as I remember to reserve the right number of bytes.

    I'll now mark this resolved.
    Thanks all !

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Putting mixed data tyes to random access files

    You don't even need to worry about the number of bytes in the type you can use the Len() function when you open the file.

    Here is an example from an old VBDos program I wrote many moons ago
    Code:
    TYPE TOWNDATA
        wall AS STRING * 1
        Text AS INTEGER
        menu AS STRING * 1
        character AS STRING * 1
        bkcolor AS INTEGER
        forecolor AS INTEGER
    END TYPE
    Town in the line below is a reference to the type above
    Code:
    OPEN "CITY1.dat" FOR RANDOM AS #3 LEN = LEN(town)
    By using Len(MyType) your record length will always be correct no matter what data types are in your UDT

  16. #16
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Putting mixed data tyes to random access files

    Quote Originally Posted by Arnoutdv View Post
    Sample:
    Code:
        Put #fID, , CLng(UBound(tVBForum) + 1)
        Put #fID, , tVBForum
      
        Get #fID, , lNofRecords
        ReDim tVBForum(lNofRecords - 1)
        Get #fID, , tVBForum
    Get #fID, , lNofRecords ...

    This is new to me, lNofRecords is returning the number of bytes, while (Get #fID, , tVBForum) is returning the data. One is Long and one is UTD..... entered in the same position and returning different values...

    I read the msdn documentation that came with my VB6 but they didn't bring this explanation.

  17. #17
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: [RESOLVED] Putting mixed data tyes to random access files

    No, it’s about a long.
    The first put statement writes a long, the first get statement reads a long

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width