Share via


5.4.5.1 Open Statement

An <open-statement> associates a file number with an external data file and establishes the processing modes used to access the data file.

 open-statement = "Open" path-name [mode-clause] [access-clause] [lock] "As" file-number [len-clause] 
  
 path-name = expression 
 mode-clause = "For" mode 
 mode = "Append" / "Binary" / "Input" / "Output" / "Random" 
 access-clause = "Access" access 
 access = "Read" / "Write" / ("Read" "Write") 
 lock = "Shared" / ("Lock"  "Read") / ("Lock" "Write") / ("Lock" "Read" "Write") 
  
 len-clause = "Len" "=" rec-length 
 rec-length = expression 

Static Semantics.

§ If there is no <mode-clause> the effect is as if there were a <mode-clause> where <mode> is keyword Random. If there is no <access-clause> the effect is as if there were an <access-clause> where <access> is determined by the value of <mode>, according to the following table:

 

Value of <mode>

File Access Type

Implied value of <access>

Append

Character

Read Write

Binary

Binary

Read Write

Input

Character

Read

Output

Character

Write

Random

Random

Read Write

 

§ If <mode> is the keyword Output then <access> MUST consist of the keyword Write. If <mode> is the keyword Input then <access> MUST be the keyword Read. If <mode> is the keyword Append then <access> MUST be either the keyword sequence Read Write or the keyword Write.

§ If there is no <lock> element, the effect is as if <lock> is the keyword Shared.

§ If no <len-clause> is present, the effect is as if there were a <len-clause> with <rec-length> equal to the Integer data value 0.

 

Runtime Semantics.

§ The <open-statement> creates an association between a file number (section 5.4.5) specified via <file-number> and an external data file identified by the <path-name>, such that occurrences of that same file number as the <file number> in subsequently executed file statements are interpreted as references to the associated external data file. Such a file number for which an external association has been successfully established by an <open-statement> is said to be currently open.

§ An <open-statement> cannot remap or change the <mode>, <access>, or <lock> of an already in-use <file-number>; the association between integer file number and an external data file remains in effect until they are explicitly disassociated using a <close-statement>.

§ If an <open-statement> fails to access the underlying file for any reason, an error is generated.

§ The value of <path-name> MUST have a data value that is Let-coercible to the declared type String. The coerced String data value MUST conform to the implementation-defined syntax for external file identifiers.

§ The Let-coerced String data value of <path-name> is combined with the current drive value (see the ChDrive function in section 6.1.2.5.2.2) and current directory value in an implementation defined manner to obtain a complete path specification.

§ If the external file specified by the complete path specification <path-name> does not exist, an attempt is made to create the external file unless <mode> is the keyword Input, in which case an error is generated.

§ If the file is already opened by another process or the system cannot provide the locks requested by <lock>, then the operation fails and an error (number 70, "Permission denied") is generated. If the file cannot be created, for any reason, an error (number 75, "Path/File access error" is generated.

§ An error (number 55, "File already open") is generated if the <file-number> integer value already has an external file association that was established by a previously executed <open-statement>.

§ The expression in a <len-clause> production MUST evaluate to a data value that is Let-coercible to declared type Integer in the inclusive range 1 to 32,767. The <len-clause> is ignored if <mode> is Binary.

§ If <mode> is Append or Output, the path specification MUST NOT identify an external file that currently has a file number association that was established by a previously executed <open-statement>. If an external file has associations with multiple file number associations then the interaction of file statements using the different file numbers is implementation defined. The value of <mode> controls how data is read from, and written to, the file. When <mode> is Random, the file is divided into multiple records of a fixed size, numbered 1 through n. 

 

Value of <mode>

Description

Append

Data can be read from the file, and any data written to the file is added at the end

Binary

Data can be read from the file, and any data written to the file replaces old data

Input

Data can only be sequentially read from the file

Output

Data can only be sequentially written to the file

Random

Data can be read from or written to the file in chunks (records) of a certain size

 

§ The <access> element defines what operations can be performed on an open file number by subsequently executed file statements. The list of which operations are valid in each combination of <mode> and <access> is outlined by the following table:

Statement/Mode

Append

Binary

Input

Output

Random

Get #

-

R, RW

-

-

R, RW

Put #

-

RW, W

-

-

RW, W

Input #

-

R, RW

R

-

-

Line Input #

-

R, RW

R

-

-

Print #

RW, W

-

-

W

-

Write #

RW, W

-

-

W

-

Seek

RW, W

R, RW, W

R

W

R, RW, W

Width #

RW, W

R, RW, W

R

W

R, RW, W

Lock

RW, W

R, RW, W

R

W

R, RW, W

Unlock

RW, W

R, RW, W

R

W

R, RW, W

Key:

R The statement can be used on a <file-number> where <access> is Read

W The statement can be used on a <file-number> where <access> is Write

RW The statement can be used on a <file-number> where <access> is Read/Write - The statement can never be used in the current mode

 

§ The <lock> element defines whether or not agents external to this VBA Environment can access the external data file identified by the complete path specification while the file number association established by this <open-statement> is in effect. The nature of such external agents and mechanisms they might use to access an external data file are implementation defined. The exact interpretation of the <lock> specification is implementation defined but the general intent of the possible lock modes are defined by the following table:

 

Lock Type

Description

Shared

External agents can access the file for read and write operations

Lock Read

External agents cannot read from the file

Lock Write

External agents cannot write to the file

Lock Read Write

External agents cannot open the file

 

§ The value of <rec-length> is ignored when <mode> is Binary. If <mode> is Random, the value of <rec-length> specifies the sum of the individual sizes of the data types that will be read from the file (in bytes). If <rec-length> is unspecified when <mode> is Random, the effect is as if <rec-length> is 128. For all other values of <mode>, <rec-length> specifies the number of characters to read in each individual read operation.

§ If <mode> is Random, when a file is opened the file-pointer-position points at the first record. Otherwise, the file-pointer-position points at the first byte in the file.