Open Statement |
No
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.
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.
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 that can be taken against the file in the current process, by specifying Read, Write, or Read Write. The default is Read Write.
The lock argument allows you to restrict the operations performed on the open file by other processes, as shown in the following table:
Lock Type | Description |
---|---|
Shared | Other processes can open the file for both read and write operations. |
Lock Read | Other processes can only write to the file. |
Lock Write | Other processes can only read from the file. |
Lock Read Write | Other processes can't open the file. |
The reclength argument is treated differently, depending upon the open mode, as the following table shows:
Open Mode | Meaning of Len Is... |
---|---|
Random | Length in bytes of each record |
Binary | Ignored |
Append/input/output | The number of characters to buffer |
The following example opens a data file for random access and assigns a record from that file to a user-defined type.
Private Function getCustomerData(sFileName As String, _ lCustNo As Long) As Boolean Dim iFile As Integer iFile = FreeFile Open sFileName For Random As #iFile Len = Len(udtCustomer) Get #iFile, lCustNo, udtCurrentCustomer Close #iFile End Function
To avoid using the file number of an already open file and generating an error, use the FreeFile function to allocate the next available file number.
You can open an already opened file using a different file number in binary, input, and random modes. However, you must close a file opened using append or output before you can open it with a different file number.