5.4.5 File Statements

VBA file statements support the transfer of data between VBA programs and external data files.

 file-statement = open-statement / close-statement / seek-statement / lock-statement / unlock-statement / line-input-statement / width-statement / print-statement / write-statement / input-statement / put-statement / get-statement  

The exact natures of external data files and the manner in which they are identified is host defined. Within a VBA program, external data files are identified using file numbers. A file number is an integer in the inclusive range of 1 to 511. The association between external data files and VBA file numbers is made using the VBA Open statement.

VBA file statements support external files using various alternative modes of data representations and structures. Data can be represented using either a textual or binary representation. External file data can be structured as fixed length records, variable length text lines, or as unstructured sequences of characters or bytes. The external encoding of character data is host-defined.

VBA defines three modes of interacting with files: character mode, binary mode and random mode. In character mode, external files are treated as sequences of characters, and data values are stored and accessed using textual representations of the values. For example, the integer value 123 would be literally represented in a file as the character 1, followed by the character 2, followed by the character 3.

Character mode files are divided into lines each of which is terminated by an implementation dependent line termination sequence consisting of one or more characters that marks the end of a line. For output purposes a character mode file can have a maximum line width which is the maximum number of characters that can be output to a single line of the file. Within a line, characters positions are identified as numbered columns. The left-most column of a line is column 1. A line is also logically divided into a sequence of fourteen-character wide print zones.

In binary mode, data values are stored and accessed using an implementation-defined binary encoding. For example, the integer value 123 would be represented using its implementation-defined binary representation. An example of this would be as a four byte binary twos-complement integer in little endian order.

In random mode, values are represented in a file the same way as character mode, but instead of being accessed as a sequential data stream, files opened in random mode are dealt with one record at a time. A record is a fixed size structure of binary-encoded data values. Files in random mode contain a series of records, numbered 1 through n.

A file-pointer-position is defined as the location of the next record or byte to be used in a read or write operation on a file number. The file-pointer-position of the beginning of a fine is 1. For a character mode file, the current line is the line of the file that contains the current file-pointer-position. The current line position is 1 plus the current file-pointer-position minus the file-pointer position of the first character of the current line.