Close Statement

Named Arguments

162 Chapter 7- The Language Reference


Close [filenumber] filenumber

Use: Optional

Data Type: Integer

The file number used when opening the file in the Open statement. Description

Closes a file opened with the Open statement. Rules at a Glance

• If filenumber is omitted, all open files are closed.

• If the file you are closing was opened for output or append, the remaining data in the I/O buffer is written to the file. The memory buffer is then reclaimed.

• When the Close statement is executed, the file number used is freed for further use.

• The hash (#) sign in front of the file number is optional.

• filenumber can either be a numeric constant (e.g., #1) or a numeric variable. Example

Dim intFileNo as Integer intFileNo = FreeFile()

Open sFileNameString For Output As #intFileNo Write #intFileNo, sOutputString Close #intFileNo

Programming Tips & Gotchas

• You can close more than one file at once with the Close method, by specifying the file numbers as a comma-delimited list, as shown below:

• The Close statement doesn't check first to see if there is a file associated with the given file number. Therefore, no error occurs if you use the Close statement with a nonexistent file number. The drawback to this is that you may think you have closed a file inadvertently when in fact you haven't, thereby leaving the file open, as this snippet demonstrates:

Dim sFilename As String sFilename = "testtext.txt"

Open sFilename For Output As #1 Write #1, sFilename Close #2

'just to prove the file is still open Write #1, sFilename

See Also

Open Statement, File System Objects

Close Statement 163

Was this article helpful?

0 0

Post a comment