CitectVBA Programming Reference > CitectVBA Function Reference > File I/O Functions > Open

Open

Open statement enables input/output (I/O) to a disk file.

The required File argument must be a valid string expression representing a valid file name. File may contain a DOS path structure including directory or folder names, and a drive letter.

The required Mode argument must be a valid keyword specifying the file I/O mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.

The optional Access argument must be a valid keyword specifying the operations permitted on the open file: Read, Write, or Read Write.

The optional Lock argument must be a valid keyword specifying the operations permitted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.

The required FileNum argument must contain an Integer representing the number that will be associated with the file. This is the file system reference number supplied by the FreeFile statement that can be used in functions such as Get #, Input #, Line Input #, Print #, and Write #. In Binary, Input, and Random modes, you can open a file using a different file number without first closing the file. In Append and Output modes, you must close a file before opening it with a different file number.

Note: The file system tracks all open files and the current position of access within every file. Every statement or function that accesses the data within a file, alters the current position within that file. The Loc function can be used to determine the current position within an open file.

The optional RecLen argument must be a number less than or equal to 32,767 (bytes). For files opened for Random access, this value is the record length. For sequential files, this value is the number of characters buffered. The Len clause is ignored if mode is Binary.

You must open a file before any I/O operation can be performed on it. Open allocates a buffer for I/O to the file and determines the mode of access to use with the buffer. If the file is already opened by another process and the specified type of access is not allowed, the Open operation will not succeed and an error message will be generated.

If the file specified by pathname doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.

Syntax

Open(FileFor ModeAccess Access Lock As #FileNum Len=RecLen)

File:

A string or expression that can represent a valid file name, and may include a DOS path structure including directory or folder names, and a drive letter.

Mode:

A CitectVBA keyword specifying the file I/O mode: Append, Binary, Input, Output, or Random.

Lock:

A CitectVBA keyword specifying the operations permitted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.

Access:

A CitectVBA keyword specifying the operations permitted on the open file: Read, Write, or Read Write.

FileNum:

An Integer or numeric expression representing any valid number in the range 1 to 511 inclusive, which is referenced by the file system to be associated with an open file.

RecLen:

An Integer or numeric expression representing the byte length of a file record as a number less than or equal to 32,767.

Related Functions

Close | FileCopy | FreeFile | Kill | Name

Example

' The following code opens the file TESTFILE in sequential-input 
mode. 
Open "TESTFILE" For Input As #1
' Close before reopening in another mode.
Close #1

' This example opens the file in Binary mode for writing operations only.
Open "TESTFILE" For Binary Access Write As #1
' Close before reopening in another mode.
Close #1

' The following example opens the file in Random mode. The file contains records of the user-defined type Record.
Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type
Dim recRecord As Record ' Declare variable.
Open "TESTFILE" For Random As #1
' Close before reopening in another mode.
Close #1

' This code example opens the file for sequential output; any process can read or write to file.
Open "TESTFILE" For Output Shared As #1
' Close before reopening in another mode.
Close #1

' This code example opens the file in Binary mode for reading; other processes can't read file.
Open "TESTFILE" For Binary Access Read Lock Read As #1
' Close before reopening in another mode.
Close #1

Published June 2018