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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bad Record Length on FileGet and FilePut

Status
Not open for further replies.

scc

Programmer
Apr 30, 2001
218
US
I've created my first VB .Net application using a sample and code I found at:
The code makes sense to me... pretty much followed what was on the website, both for the coding and the UI.

However, when the code hits any of the FileGet or FilePut commands, I get an error that says "Bad Record Length".

I'm using VB .Net version 7.1.3088. Any idea on why this won't work and how to fix it? Apparently it doesn't convert the structure lenth properly.

Thanks in advance!

Here's my code from the Public Class Form1:
Code:
    'global variable to keep position of related record
    Dim Position As Integer
    Dim FileNum As Integer


    'create record type called "Person"
    Structure Person
        Dim ID As Integer
        Dim Name As String
        Dim Surname As String
    End Structure

    'find the last record number in the random file
    Private Function FindLastRecordNo() As Integer
        Dim Temp As Person, FileNumber As Integer

        'Get the available file number
        FileNumber = FreeFile()

        'Open file
        FileOpen(FileNumber, TextBox1.Text, OpenMode.Random, _
             OpenAccess.Read, , Len(Temp))

        FindLastRecordNo = 1

        'Check for EOF
        Do While Not EOF(FileNumber)
            'get a record from the random file
            FileGet(FileNumber, Temp, )

            FindLastRecordNo = FindLastRecordNo + 1

        Loop

        'close the file
        FileClose(FileNumber)

    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'EXIT button terminates the application
        End
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'CREATE button creates a new randome file using the name in textbox1
        Dim RecLength As Long, Employee As Person

        'get next available file number
        FileNum = FreeFile()
        'open the new random file using the name in textbox1
        FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee))

        'close the file
        FileClose(FileNum)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'INSERT button will save the new record at the end of the random file
        Dim Employee As Person, LastRecord As Integer

        'find last record number of random file
        LastRecord = FindLastRecordNo()

        FileNum = FreeFile()
        FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee))
        Employee.ID = Val(TextBox2.Text)
        Employee.Name = TextBox3.Text
        Employee.Surname = TextBox4.Text

        'save the Employee record into the random file as a last record
        FilePut(FileNum, Employee, LastRecord)

        'close file
        FileClose(FileNum)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'READ button reads all the records in the random file and puts them into the Listbox one by one
        Dim Employee As Person, Count As Integer, Temp As String

        FileNum = FreeFile()
        FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee))
        Count = 1
        ListBox1.Items.Clear()

        Do While Not EOF(FileNum)
            'read the record at the position count
            FileGet(FileNum, Employee, Count)

            'str() is used to convert integer to a string
            Temp = Str(Employee.ID) + " " + Employee.Name + _
            " " + Employee.Surname

            'add the string into the listbox
            ListBox1.Items.Add(Temp)
            Count = Count + 1
        Loop

        'close file
        FileClose(FileNum)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        'FIND button searches all records in random file until it 
        'find the Employee ID record that is equal to the ID
        'in Textbox5
        Dim Employee As Person
        FileNum = FreeFile()
        FileOpen(FileNum, TextBox1.Text, _
              OpenMode.Random, , , Len(Employee))

        Do While Not EOF(FileNum)
            FileGet(FileNum, Employee, )
            If Employee.ID = Val(TextBox5.Text) Then
                'keep current record position
                'to use in other operations such as Update or Delete
                Position = Loc(FileNum)

                'change enable status for some textboxes
                TextBox4.Enabled = False
                TextBox6.Enabled = True
                TextBox7.Enabled = True
                Button5.Enabled = True
                Button6.Enabled = False
                Button7.Enabled = True

                'display Employee name and surname
                TextBox6.Text = Employee.Name
                TextBox7.Text = Employee.Surname
                Exit Do
            End If
        Loop

        'close file
        FileClose(FileNum)

    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'UPDATE button is used to update an employee's information
        'based on the employee found using the Find button/procedure
        Dim Employee As Person

        FileNum = FreeFile()
        FileOpen(FileNum, TextBox1.Text, _
            OpenMode.Random, , , Len(Employee))

        'move the pointer to any position of random file
        Seek(FileNum, Position)

        'update info of an employee
        Employee.ID = Val(TextBox5.Text)
        Employee.Name = TextBox6.Text
        Employee.Surname = TextBox7.Text
        FilePut(FileNum, Employee)
        FileClose(FileNum)

        'change the enable status of some textboxes and clear value
        Call ResetTextBoxes()

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        'DELETE button is used to delete a record
        'Code opens the random file and one temporary file.  
        'All records, except the record to be deleted 
        '(its position number is kept by the Position variable)
        'is copied into the temporary file. The random file is then 
        'deleted and the temporary file is renamed the same as the 
        'random file that was deleted

        Dim Employee As Person, FileNum1, FileNum2 As Integer
        FileNum1 = FreeFile()
        FileOpen(FileNum1, TextBox1.Text, OpenMode.Output, _
        OpenAccess.Read, , Len(Employee))

        'filenum2 is for a temporary file
        FileNum2 = FreeFile()
        FileOpen(FileNum2, "abcdefg.tnt", OpenMode.Random, _
                 OpenAccess.Write, , Len(Employee))

        Do While Not EOF(FileNum1)
            'Position-1 is used in the Else statement because 
            'the FileGet() function reads the next record and 
            'we don't want to write FilePut()) that record to 
            'the temporary file
            If (Loc(FileNum1) <> Position - 1) Then
                FileGet(FileNum1, Employee, )
                FilePut(FileNum2, Employee, )
            Else
                FileGet(FileNum1, Employee, )
            End If
        Loop

        'close the files
        FileClose(FileNum1)
        FileClose(FileNum2)

        'delete the original random file
        Kill(TextBox1.Text)

        'rename the temporary file by giving th esame name as the 
        'deleted random file
        Rename("abcdefg.tnt", TextBox1.Text)

        'change the enable status of some textboxes and clear value
        Call ResetTextBoxes()

    End Sub


    Private Sub ResetTextBoxes()
        'change the enable status of some textboxes and clear value
        TextBox5.Enabled = True
        TextBox6.Enabled = False
        TextBox7.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = True
        Button7.Enabled = False
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top