Learning the File Modes
Notice that the Mode value in the Open statement does not have a data type
prefix before it. Mode must be a special keyword that indicates the file's mode of
access. Table 12.1 explains the values you can supply for the Mode argument.
Table 12.1. Use one of these Open statement Mode values.
Mode |
Description |
Append |
Opens a file number to a file for sequential output, beginning at the end of the file
if the file exists. If the file doesn't exist, Visual Basic creates the file. Append never
overwrites existing file data. |
Binary |
Opens a file number to a file for binary data access. In Binary mode, you can access a
file at the byte level, meaning that you can write and read individual bytes to and from
the file. |
Input |
Opens a file number to a file for sequential input, starting at the beginning of the
file. Data is read in the same order it was sent to the file. |
Output |
Opens a file number to a file for sequential output, starting at the beginning of the
file. If the file doesn't exist when you issue the Open statement, Visual Basic
creates the file; otherwise, Visual Basic overwrites the file. |
Random |
Opens a file number to a file for random read and write access. This mode allows data
to be read from and written to a file at any specific record boundary. |
For Mode isn't required when using the Open statement. If you don't include the
mode, Visual Basic assumes Random and inserts the For Random mode for you. The following
statements demonstrate how to use various modes for opening a file:
Open "filInpdt.txt" For Input As #1
Open "Append.txt" For Append As #1
Open "Output.txt" For Output As #1
Open "Random.txt" For Random As #1
The last statement is equivalent to the following:
Open "Random.txt" As #1

Be sure to set up error-handling logic using the On Error Goto statement that you
learned about in Day 9 "The Dialog Control." Anytime you open or access a file,
an error might result. Good error-handling logic will help your application exit
gracefully from the problem rather than burden your users with nasty runtime error
messages.
