OPEN

OPEN(pathname For mode [Access access] [lock] As [#] filenumber [Len = reclength])

Opens a text file or CSV file.


pathnameThe file name which can include folder and even drive
modeThe type of access:
Input - Sequential Access that allows read only
Output - Sequential Access that allows read and write
Append - Sequential Access that allows read and write to the end of the file
Binary - Binary Access that allows read and write to any byte position in a file
Random - Random Access that allows read and write to a file without closing it
access(Optional) The keyword to specify the type of operation:
Read
Write
Read Write
lock(Optional) The keyword to specify the type of lock:
Shared
Lock Read
Lock Write
Lock Read Write
filenumberThe number of the file (Integer).
reclength(Optional) The length of a record for Random access and the buffer size for Sequential.

REMARKS
* If the file is already open then an error will occur.
* If the file specified by pathname doesn't exist, it is created when a file is opened for Output, Append, Binary or Random modes.
* If "mode" is Binary, the optional Len clause is ignored.
* If "mode" is Binary, Input or Random, you can open a file by using a different file number without first closing the file.
* If "mode" is Append or Output, you must close a file before opening it with a different file number.
* If "mode" is unspecified, the file is opened for Random access.
* Random access files keep data in records, which makes it easy to locate information quickly.
* You can use the CLOSE statement to close a text file.
* You can use the EOF function to indicate when the end of a file has been reached.
* You can use the FREEFILE function to return the next free file number.
* You can use the INPUT Function to read a number of characters from a file.
* You can use the INPUT Statement to read data from a file and assigns the data to a list of variables.
* You can use the LINE INPUT statement to read a single line from a file opened with Sequential access.
* You can use the PRINT statement to write data to a file opened with Sequential access (display formatted).
* You can use the WRITE statement to write data to a file opened with Sequential access.
* This statement is not available in Access.
* For the Microsoft documentation refer to learn.microsoft.com

Open "C:\Temp\MyText.txt" For Output As #1 
Close #1

Open "C:\Temp\MyText.txt" For Output As #1
Print #1, "one,two,three"
Write #1, "four,five,six"
Close #1

Open "C:\Temp\MyText.txt" For Input As #1
Debug.Print LOF(1)
Do Until EOF(1)
    Dim LineFromFile As String
    Line Input #1, LineFromFile

    Dim VarName1, VarName2 As Variant
    Input #1, VarName1, VarName2

    Debug.Print LineFromFile
    Debug.Print VarName1, VarName2
Loop
Close #1

Open "C:\Temp\MyText.txt" For Binary As #1
Close #1

Open "C:\Temp\MyText.txt" For Binary Access Read Lock Read As #1
Close #1

Open "C:\Temp\MyText.txt" For Append As #1
Close #1

Open "C:\Temp\MyText.txt" For Random Shared As #1 Len = 15
Close #1

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top