Table of Contents

5.4.5 File Statements

Note

This section describes the implementation of MS-VBAL §5.4.5 File Statements.

MS-VBAL describes the file I/O statements in one section, MS-VBAL §5.4.5. RD-VBA executes every statement of that section, and Name, which MS-VBAL does not specify and the VBA language reference does.

Statement MS-VBAL RD-VBAL
Open MS-VBAL §5.4.5.1 Open Statement RD-VBAL §5.4.5.1 Open Statement
Close, Reset MS-VBAL §5.4.5.2 Close and Reset Statements RD-VBAL §5.4.5.2 Close and Reset Statements
Seek MS-VBAL §5.4.5.3 Seek Statement RD-VBAL §5.4.5.3 Seek Statement
Lock MS-VBAL §5.4.5.4 Lock Statement RD-VBAL §5.4.5.4 Lock Statement
Unlock MS-VBAL §5.4.5.5 Unlock Statement RD-VBAL §5.4.5.5 Unlock Statement
Line Input # MS-VBAL §5.4.5.6 Line Input Statement RD-VBAL §5.4.5.6 Line Input Statement
Width # MS-VBAL §5.4.5.7 Width Statement RD-VBAL §5.4.5.7 Width Statement
Print # MS-VBAL §5.4.5.8 Print Statement RD-VBAL §5.4.5.8 Print Statement
Write # MS-VBAL §5.4.5.9 Write Statement RD-VBAL §5.4.5.9 Write Statement
Input # MS-VBAL §5.4.5.10 Input Statement RD-VBAL §5.4.5.10 Input Statement
Put MS-VBAL §5.4.5.11 Put Statement RD-VBAL §5.4.5.11 Put Statement
Get MS-VBAL §5.4.5.12 Get Statement RD-VBAL §5.4.5.12 Get Statement
Name Not specified. RD-VBAL §5.4.5.13 Name Statement

The AST node of each file statement is catalogued in RD-VBAL §3.4.3 File Statements. Every file statement lowers to a Simple instruction (RD-VBAL §3.5.2 Instruction).

The File-Channel Shim

All file statements run through one session-level shim: the IFileChannels / IFileChannel interfaces (RD-VBAL §2.3.1.2 Session Services).

IFileChannels holds numbered channels. Each channel records the mode it was opened under.

An administrator can restrict or redirect file I/O at the shim. A test can replace the shim with a fake.

Statement, Mode and Access

MS-VBAL §5.4.5.1 gives one statement/mode/access table for the file statements. FileStatementAccess holds that table in one place, so that the twelve file statements do not each restate it.

Channel Surfaces

A file channel has a character surface in each direction (output and input), and a record surface.

Surface Used by Interface
Character output Print #, Write # IFileChannelOutput
Character input Line Input #, Input # IFileChannelInput
Record Put, Get IFileChannel

IFileChannelOutput carries a maximum line length, which Width # sets (RD-VBAL §5.4.5.7 Width Statement).

The text format that Write # writes and Input # reads is described in RD-VBAL §5.4.5.9 Write Statement. The record format that Put writes and Get reads is described in RD-VBAL §5.4.5.11 Put Statement.

Reading Statements

Input #, Line Input # and Get Let-assign what they read to their targets. They resolve those targets with the same target resolution the Let assignment statement uses (RD-VBAL §5.4.3.8 Let Statement).

Static Semantics

What the operands of a file statement have to be declared as is FileStatementStaticSemantics's, which StatementStaticSemanticsEvaluator asks. A declared type that breaks a rule is a TypeMismatch; an operand whose declared type is not known is deferred, not rejected.

Operand Rule MS-VBAL
A file number (every statement but Print with none), a Seek position, a Lock or Unlock record number, a Width line width, a Spc or Tab number A scalar declared type: any but an array or a user-defined type. §5.4.5.1.1, .3, .4, .7, .8.1
The path name of an Open, and the Len of its record length Let-coercible to String, and to Integer. §5.4.5.1
The Access of an Open Output: Write. Input: Read. Append: Read Write or Write. Otherwise VBC09334. §5.4.5.1
The variable of a Line Input # A variable (VBC09333), declared as a String or a Variant. §5.4.5.6
Each variable of an Input # A variable, not declared as an Object or a class. §5.4.5.10
The data of a Put Not declared as an Object, a class, or a user-defined type that has one in it. §5.4.5.11
The variable of a Get A variable, declared as the data of a Put is. §5.4.5.12
Each operand of a Name Let-coercible to String. (RD-VBAL §5.4.5.13; not in MS-VBAL.) -

Only an expression that is certainly not a variable is reported as one (VBC09333): a literal, an operator's result, a constant.

Implementation

Name Role
FileStatementStaticSemantics The static rules above, for every file statement and for Name.
IFileChannels The session-level shim every file statement runs through: the numbered channels, each with the mode it was opened under.
IFileChannel One numbered channel. Its record surface is the one Put and Get use.
IFileChannelOutput The character output surface of a channel, used by Print # and Write #. It carries the maximum line length Width # sets.
IFileChannelInput The character input surface of a channel, used by Line Input # and Input #.
FileStatementAccess MS-VBAL §5.4.5.1's statement/mode/access table, held in one place.
LetAssignmentEvaluator.TryResolveTarget The target resolution Input #, Line Input # and Get use: the same target resolution the Let assignment statement uses.
RecordDataFormat The Put/Get record format (RD-VBAL §5.4.5.11 Put Statement).

⏮️ RD-VBAL §5.4.4.3 Error Statement | ⏭️ RD-VBAL §5.4.5.1 Open Statement