Open Statement

Allows Input/Output operations with a file.

Syntax

Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

The Open statement syntax contains the following parts:

Part Description
pathname Required. String expression indicating the filename. The path can contain directory and drive name.
mode Required. Keyword indicating the file open mode: Append, Binary, Input, Output or Random. By default, a file is opened in the Random access mode.
access Optional. Keyword specifying operations allowed with the opened file: Read, Write or Read Write.
lock Optional. Keyword specifying operations that other processes can perform on the opened file: Shared, Lock Read, Lock Write and Lock Read Write.
filenumber Required. File number may range from 1 to 511 inclusive. To find next free file number use the FreeFile function.
reclength

Optional. Number less or equal 32 767 (bytes). For files opened in the Random mode this value is the lenght of record. For files with serial access this value is the number of symbols read into the buffer.

Remarks

A file must be open in order to perform input/output operations. The Open statement reserves the input/output buffer for the file and sets the buffer usage mode.
If the path argument describes a file that doesn't exist, such file will be created when opening in Append, Binary, Output or Random modes.
If the file is already opened by some other process and the specified access mode is not allowed, the Open statement will not be executed and an error will be generated.

If the mode argument is set to Binary, the Len parameter is ignored.

Example

This example shows different ways of using the Open statement for file input/output operations.

Opening TESTFILE for reading.

Open "TESTFILE" For Input As #1
' Close file before re-opening in another mode.
Close #1

Opening the file in the Binary mode for writing only.

Open "TESTFILE" For Binary Access Write As #1
' Close file before re-opening in another mode.
Close #1

The following commands open the file for ... output (serial output); any process can read from or write to the file.

Open "TESTFILE" For Output Shared As #1
' Close file before re-opening in another mode.
Close #1

The following commands open the file in the Binary mode for reading; other processes can't read from this file.

Open "TESTFILE" For Binary Access Read Lock Read As #1
' Close the file. Close #1

 

See Also

Recording Data in a FileClose Statement , FreeFile Function