Open Statement

Named Arguments

No

Syntax

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

pathname

Use: Required

Data Type: String

The name of the file to open, along with an optional path.

mode

Use: Required

Data Type: Keyword

The file access mode: append, binary, input, output, or random.

access

Use: Optional

Data Type: Keyword

Specifies the allowable operations by the current process.

lock

Use: Optional

Type: Keyword

Specifies the allowable operations by other processes.

filenumber

Use: Required

Data Type: Integer

A valid file number between 1 and 511.

reclength

Use: Optional

Data Type: Integer

The length of the record or I/O buffer.

Description

Before reading from and/or writing to a disk file, you must first open the file using the Open statement. The Open statement allocates memory for the I/O buffer and optionally sets access locks on the file.

Rules at a Glance

  • pathname may include the directory or folder and drive; if these are omitted, the file is assumed to reside in the current working directory. If pathname does include drive and path information, this may take the form of a path relative to the local system or a UNC path.

  • The default mode for opening a disk file (when mode isn't specified) is random.

  • If the specified file doesn't exist when opening in input mode, an error occurs.

  • A new file is created if the specified file doesn't exist when opening in append, binary, output, or random mode.

  • access allows you to restrict the actions ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.