Private Sub Form_Load()
Dim FileName As String
Dim FileNum As Integer
gIndex = 0 'initializes number of students to zero
'Below, variables are set for opening file
FileNum = FreeFile 'Function to find free file number
FileName = App.Path & "\class.txt"
Open FileName For Input As FileNum 'File is opened
Do While EOF(FileNum) = False 'loop that reads all data from file
gIndex = gIndex + 1
ReDim Preserve gStudents(1 To gIndex)
Input #FileNum, gStudents(gIndex).Name, gStudents(gIndex).ID, gStudents(gIndex).score(1), gStudents(gIndex).score(2), gStudents(gIndex).score(3)
Loop
Close #FileNum 'File is closed after all reading has been completed
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim FileName As String
Dim FileNum As Integer
Dim Counter As Integer
FileNum = FreeFile 'Finds free file number
FileName = App.Path & "\class.txt"
Open FileName For Output As #FileNum
For Counter = 1 To gIndex
Write #FileNum, gStudents(Counter).Name, gStudents(Counter).ID, gStudents(Counter).score(1), gStudents(Counter).score(2), gStudents(Counter).score(3)
Next Counter 'counter used to store all data that has been inputted
Close #FileNum
End Sub
There is some sample code from one of my projects.