' Ch. 9 Demo Program #8 ' Mr. Minich ' Purpose - Comparing the use of sequential and random access files
Option Explicit Private Sub Form_Load() Open "Z:\Temp\Info.txt" For Input As #1 ' sequential access files need to be opened in Input, Output, or Append type of access ' Mr. Minich prefers to use the .txt extension for sequential access files Dim J As Integer ' loop variable Dim strInfo(1 To 10) As String ' info from sequential access file For J = 1 To 9 ' reading info from sequential access file Input #1, strInfo(J) ' using the Input command Next J ' there is no easy way to write a single piece of information to ' a specific position within a sequential access file Open "Z:\Temp\Info.dat" For Random Access Read As #2 Len = 15
' random access files need to be opened in Read or Write mode ' and you must specify the length of each record in characters ' Mr. Minich prefers to use the .dat extension with random access files Dim X As Integer ' loop variable Dim strMoreInfo(1 To 10) As String ' info from random access file For X = 1 To 9 ' reading info from random access file Get #2, J, strMoreInfo(X) ' using the Get command Next X Dim strName As String ' piece of info to write to a file strName = "Bill" Put #2, 7, strName ' writing a piece of info ("Bill") to the 7th End Sub ' record of the random access file ' In summary, there are two common types of access with which to open each type of file with: ' ' There are two file modes that you can work in: sequential or random access ' ' There are three types of access with each mode: ' sequential access files - Input, Output & Append ' random access files - Read, Write, & Read Write ' There are two operations used with each file mode as well: ' sequential access files - Input & Print ' random access files - Get & Put ' In sequential access files, individual pieces of information are usually stored on ' on separate lines or separated by commas. ' In random access files, individual pieces of information are stored in records with ' each record being the same size. ' We often use NotePad to view or edit sequential access files. But random access files ' cannot and should not be viewed and editted.