The File control is an ActiveX control that enables you to read and write files. The file modes—Input, Output, Append, Random, or Binary—determine how a File control interprets data in a file.
The File control uses the following properties to return information about an open file: Attr, EOF, Loc, LOF, and Seek. The Attr property returns the file mode that was used to open the file. Also, the File control maintains the position where the next read or write will occur in an open file. The EOF, Loc, and Seek properties return information about the current file position. The Seek property changes the current position within a file.
The methods you can use to read and write files depend on the file mode. The following table shows the file mode constants and methods for each file mode.
File Mode |
Constant |
Method |
Input | fsModeInput | Input, InputB, LineInputString, InputFields |
Output | fsModeOutput | LinePrint, WriteFields |
Append | fsModeAppend | LinePrint, WriteFields |
Random | fsModeRandom | Get, Put |
Binary | fsModeBinary | Get, Put, Input, InputB |
Before you can read and write a file, you must open it with the Open method. Opening a file in Append mode sets the current position to the end of the file or creates a new file if one does not already exist. All other file modes set the current position to the beginning of the file. The Output file mode erases the contents of an existing file, so use caution with fsModeOutput. In addition, the Random file mode requires that you specify a record length when you open a file.
To open a file in a different file mode, you first must close it with the Close method. You can determine if the File control already has a file open by examining the Attr property. The Attr property returns 0 if the file is closed.
The following code example shows how to open a file.
Private Sub Command6_Click()
File1.Open "Append", fsModeAppend
File1.LinePrint "Appending..."
MsgBox File1.LOF, , "The length is ..."
File1.Close
End Sub