Results 1 to 4 of 4

Thread: Put Statment How do you add a record to the end of file i.e after the last record

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Unhappy

    My problem is with the get and put statement. How do you add to the end of file i.e after the last record and how do you get the last record in the file?

    The line
    LastRecord = LastRecord + 1
    Put FileNum, LastRecord, Q2
    is what Microsoft says to do but it does not work. I need a way to get the last record position then add 1 to it before I do the Put. I tried all types of loops and I just can't seem to get it right. If anyone knows how to do this please help
    Thanks Troy


    Here is my Bas:

    Option Explicit
    Type Question
    Ques As String * 150
    Answer1 As String * 50
    Answer2 As String * 50
    Answer3 As String * 50
    Answer4 As String * 50
    Answer5 As String * 50 'Correct answer
    ID As Integer

    End Type

    ' A record variable.
    Public Q2 As Question
    ' Tracks the current record.
    Public Position As Long
    ' The number of the last record in the file.
    Public LastRecord As Long


    Below is my code:

    Public Function RndPut()
    Dim FileNum As Integer, RecLength As Long, Q2 As Question
    Dim FileName As String
    ' Calculate the length of each record.
    RecLength = LenB(Q2)
    ' Get the next available file number.
    FileNum = FreeFile
    FileName = App.Path & "\Question.dat"
    Open FileName For Random As FileNum Len = RecLength

    'Position = List1.Text
    Q2.Ques = txtQuestion.Text
    Q2.Answer1 = Text1.Text
    Q2.Answer2 = Text2.Text
    Q2.Answer3 = Text3.Text
    Q2.Answer4 = Text4.Text
    Q2.Answer5 = Text5.Text
    'Q2.ID = txtID
    LastRecord = LastRecord + 1
    Put FileNum, LastRecord, Q2
    TMacPherson
    MIS Systems Engineer
    [email protected]


  2. #2
    New Member
    Join Date
    Aug 2000
    Posts
    15
    This should get you the the record number of the last record:

    Code:
    lastrecord=format(filelen(filename)/reclength,"####")
    This will work if your records are all fixed length, and its looks like they are to me.
    That's not a BUG, It's a feature!



    VB6 Professional Service Pack 3
    Windows CE Embedded Visual Tools 3

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Put with no record/byte number

    I thought that Put Statement without Record or Byte number resulted in Put after last byte in File (or at last Seek position). At least that is what my Language reference manual says. You need two commas to indicate missing parameter.

    I think you might have to Open in Append Mode if not a binary File (Not sure about this).
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Thumbs up Put staement how do you add a record to the end of a file i.e after the last record

    Peak thanks but that didn't work it always returned 2.

    Guv thanks for the idea I looked up peak on the MSDN and found what I needed. All I needed was this one line I think?

    MaxSize = LOF(1) \ Len(Q2) ' Get number of records in file.

    Didn't need this stuff below
    ' The loop reads all records starting from the last.
    'For RecordNumber = MaxSize To 1 Step -1
    ' Seek FileNum, RecordNumber ' Set position.
    ' Get FileNum, , Q2 ' Read record.
    'Next RecordNumber
    TMacPherson
    MIS Systems Engineer
    [email protected]


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