See Also Example
Enables input/output (I/O) to a file.
Syntax
Open file [For mode] [Access access] [lock] As [#]filenumber [Len = reclen]
Remarks
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 used with the buffer.
The Open statement has these parts:
Part Description
file File name or path.
mode Reserved word that specifies the file mode: Append, Binary, Input, Output, Random.
access Reserved word that specifies which operations are permitted on the open file: Read, Write, Read Write.
lock Reserved word that specifies which operations are permitted on the open file by other processes: Shared, Lock Read, Lock Write, Lock Read Write.
filenumber Integer expression with a value between 1 and 255, inclusive. When an Open statement is executed, filenumber is associated with the file as long as it is open. Other I/O statements can use the number to refer to the file.
reclen For files opened for random access, the record length; for sequential files, the number of characters buffered. The argument reclen is a positive integer expression less than or equal to 32,767 bytes.
If file doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.
The argument mode is a reserved word that specifies one of the following file modes.
Mode Description
Random Random-access file mode (default). In this mode, if no Access clause is present, three attempts are made to open the file when the Open statement is executed. Access is attempted in the following order:
1st. Read and write
2nd. Write only
3rd. Read only
Binary Binary file mode. In Binary mode, you can read and write information to any byte position in the file using Get and Put statements. If no Access clause is present, three attempts are made to open the file, following the same order as for Random files.
Input Sequential input mode.
Output Sequential output mode.
Append Sequential output mode. Append sets the file pointer to the end of the file. A Print # or Write # statement then extends (appends to) the file.
If the argument mode is omitted, the default mode (Random) is used.
The argument access is a reserved word that specifies the operations that can be performed on the opened file. If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and a Permission denied error occurs.
The Access clause works only if you are using a version of MS-DOS that supports networking (MS-DOS version 3.1 or later). You (or the network startup program) must also run the SHARE.EXE program to enable locking operations. If you use the Access clause with a version of MS-DOS that doesn't support networking, a Feature unavailable error occurs.
The argument access can be one of the following reserved words.
Access type Description
Read Opens the file for reading only.
Write Opens the file for writing only.
Read Write Opens the file for both reading and writing. This mode is valid only for Random and Binary files and files opened for Append mode.
The lock clause works in a multiuser or multiprocessing environment to restrict access by other processes or users to an open file. The argument can be one of the following reserved words.
Lock type Description
Shared Any process on any machine can read from or write to this file.
Lock Read No other process is granted read access to this file. This lock is applied only if no other process has a previous read access to the file.
Lock Write No other process is granted write access to this file. This lock is applied only if no other process has a previous write access to the file.
Lock Read Write No other process is granted either read or write access to this file. This access is granted only if read or write access has not already been granted to another process, or if a Lock Read or Lock Write is not already in place.
If you don't specify a lock type, the file can be opened for reading and writing any number of times by the statement, but other operations are denied access to the file while it is open.
When Open is restricted by a previous process, a Permission denied error occurs.
The argument reclen is an integer expression less than or equal to 32,767 bytes. It specifies settings for Random and sequential files.
For Random files, the argument reclen sets the record length (the number of characters in a record). The default record length is 128 bytes.
For sequential files, the argument reclen specifies the number of characters to be loaded into the buffer before the buffer is written to or read from the disk. A larger buffer uses more memory but provides faster file I/O. Conversely, a smaller buffer leaves more room in memory but slows I/O. The default buffer size is 512 bytes.
The Len clause and reclen are ignored if mode is Binary. For sequential files, reclen need not correspond to an individual record size, because a sequential file may have records of different sizes.
Note 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.
Close Statement
FreeFile Function
This example uses Open to open a file for output. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim FName, FNum, I, Msg, TestString ' Declare variables.
TestString = "The quick brown fox" ' Create test string.
For I = 1 To 5
FNum = FreeFile ' Determine file number.
FName = "TEST" & FNum & ".DAT"
Open FName For Output As FNum ' Open file.
Print #I, TestString ' Write string to file.
Next I
Close ' Close all files.
Msg = "Several test files have been created on your disk. "
Msg = Msg & "Choose OK to remove the test files."
MsgBox Msg
Kill "TEST?.DAT" ' Remove test files.
End Sub