title: Random access file visual basic 2012
body: <p>I am having trouble making my record number unique in visual basic 2012 the code i have so far is overwriting the file that was previously saved??</p>
  
  <p>my module</p>
  
  <pre><code>Imports System.IO
  Module AssModule
      Structure employee
          ' Public Name As String
          ' Public ID As String
  
          &lt;VBFixedString(30)&gt; Dim name As String ' vb fixed string
          &lt;VBFixedString(5)&gt; Dim id As String ' vb fixed string
  
  
  
      End Structure
  
      Structure transaction
          &lt;VBFixedString(5)&gt; Dim id As String ' vb fixed string
          Dim time As Date
          &lt;VBFixedString(3)&gt; Dim type As String ' vb fixed string
      End Structure
  
      ' Public fileSize As Integer = Len(index)
      ' Public Staff(100) As employee ' staff as new name for eployeee structure
  
  
      Public myEmployee As employee
  
      Public r As Integer = 5
      Public fileNumber As Integer = FreeFile()
      Public mySizerecordnumber As Integer = Len(myEmployee)
      Public recordNumber As Integer
  
  
  
      Public index As Byte
  
  
  
      Public Sub setRecordNumber()
          Dim n As Byte = 0
          FileOpen(fileNumber, "binData.dat", OpenMode.Random, OpenAccess.Read)
          While Not EOF(fileNumber)
              n = n + 1
              FileGet(fileNumber, myEmployee, n)
          End While
          recordNumber = n
          FileClose(fileNumber)
      End Sub
  
  
  
  End Module
  </code></pre>
  
  <p>add employee button</p>
  
  <pre><code>Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
      For n = 0 To 3
          FileOpen(fileNumber, "binData.dat", OpenMode.Random, OpenAccess.Write, , mySizerecordnumber)
          myEmployee.id = txtID.Text
          myEmployee.name = txtName.Text
          recordNumber = recordNumber + 1
          FilePut(fileNumber, myEmployee, recordNumber)
          FileClose(fileNumber)
      Next
      Me.Close()
  End Sub
  </code></pre>