
Introduction
In this article, I will try to explain how to prepare an application for
working with random files using VB.NET. Before starting, I want say that this
application is just a sample for working with random files. Only the usage of
random files are concentrated on, not the functionality of the program. If this
is clear, we can start ;)
Implementation
In the first step, we are creating a new project and placing a
TabControl
on to the form. Then, create four tabs on the
TabControl
(select TabPages
property and create these
tabs) by names, �Create�, �Input�, �Output�, and �Search� as shown in Figure 1.
below.

Figure 1. TabControl on the form
In the second step, use TextBox
, Label
,
Button
, and ListBox
components and form the tab pages
as shown below.

Figure 2. Tab page �Create� |

Figure 3. Tab page �Input� |

Figure 4. Tab page �Output� |

Figure 5. Tab page �Search� |
Now, the form will look like the form in Figure 6. below. Note that the
�Exit� button is placed on the form to just terminate the application.

Figure 6. Application form
In the third step, we are starting to write source code for our application.
First of all, let�s define a global variable, record type and write a function
to find the last record number of the random file.
Dim Position As Integer
Structure Person
Dim ID As Integer
Dim Name As String
Dim Surname As String
End Structure
Private Function FindLastRecordNo() As Integer
Dim Temp As Person, FileNumber As Integer
FileNumber = FreeFile()
FileOpen(FileNumber, TextBox1.Text, OpenMode.Random, _
OpenAccess.Read, , Len(Temp))
FindLastRecordNo = 1
Do While Not EOF(FileNumber)
FileGet(FileNumber, Temp, )
FindLastRecordNo = FindLastRecordNo + 1
Loop
FileClose(FileNumber)
End Function
Here are the event procedures and explanations of important statements. Note
that �...� is used, instead of writing arguments, in each procedure title
because of easy readability. This is only for Exit button to terminate the
application.
Private Sub Button1_Click(�) Handles Button1.Click
End
End Sub
This event procedure is for Create button on the �Create� tab page.
Private Sub Button2_Click(�) Handles Button2.Click
Dim RecLength As Long, Employee As Person
FileNum = FreeFile()
FileOpen(FileNum, TextBox1.Text, _
OpenMode.Random, , , Len(Employee))
FileClose(FileNum)
End Sub
When the user clicks Insert button on the �Input� tab page, this procedure
will run and save the new record at the end of the random file.
Private Sub Button3_Click(�) Handles Button3.Click
Dim Employee As Person, LastRecord As Integer
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
FilePut(FileNum, Employee, LastRecord)
FileClose(FileNum)
End Sub
Read all records in the random file and put these records into the Listbox
one by one. This is the procedure for Read button on the �Output� tab page.
Private Sub Button4_Click(�) Handles Button4.Click
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)
FileGet(FileNum, Employee, Count)
Temp = Str(Employee.ID) + " " + Employee.Name + _
" " + Employee.Surname
ListBox1.Items.Add(Temp)
Count = Count + 1
Loop
FileClose(FileNum)
End Sub
This is the Find button on the �Search� tab page. It is searching all
records in the random file until it finds Employee ID in the record is equal to
the ID in the TextBox5 (Textbox5 is assigned to Employee Id on the �Search� tab
page.)
Private Sub Button6_Click(�) Handles Button6.Click
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
Position = Loc(FileNum)
TextBox5.Enabled = False
TextBox6.Enabled = True
TextBox7.Enabled = True
Button5.Enabled = True
Button6.Enabled = False
Button7.Enabled = True
TextBox6.Text = Employee.Name
TextBox7.Text = Employee.Surname
Exit Do
End If
Loop
FileClose(FileNum)
End Sub
This procedure is for Update button (�Search� tab page) and is to update
the record. We know the position of found record from Find procedure and we will
use this position to overwrite the information of Employee on this record.
Private Sub Button5_Click(�) Handles Button5.Click
Dim Employee As Person
FileNum = FreeFile()
FileOpen(FileNum, TextBox1.Text, _
OpenMode.Random, , , Len(Employee))
Seek(FileNum, Position)
Employee.ID = Val(TextBox5.Text)
Employee.Name = TextBox6.Text
Employee.Surname = TextBox7.Text
FilePut(FileNum, Employee, )
FileClose(FileNum)
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
This procedure is for Delete button (�Search� tab page) and is to delete
the record. In this procedure, we are opening our random file and one temporary
file. Then copying all records except the record, which its position number kept
by Position variable, into the temporary file. After that, we delete our random
file and rename the temporary file by giving the same name as deleted random
file. This name is found in the Textbox1 (�Create� tab page.)
Private Sub Button7_Click(�) Handles Button7.Click
Dim Employee As Person, FileNum1, FileNum2 As Integer
FileNum1 = FreeFile()
FileOpen(FileNum1, TextBox1.Text, OpenMode.Random, _
OpenAccess.Read, , Len(Employee))
FileNum2 = FreeFile()
FileOpen(FileNum2, "abcdefg.tnt", OpenMode.Random, _
OpenAccess.Write, , Len(Employee))
Do While Not EOF(FileNum1)
If (Loc(FileNum1) <> Position - 1) Then
FileGet(FileNum1, Employee, )
FilePut(FileNum2, Employee, )
Else
FileGet(FileNum1, Employee, )
End If
Loop
FileClose(FileNum1)
FileClose(FileNum2)
Kill(TextBox1.Text)
Rename("abcdefg.tnt", TextBox1.Text)
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
Have a nice day!