View contents of this chapter

Free File Function

You can open multiple files at once, as long as you assign each file a different file number. You must keep a track of the next available number. VB provides the FreeFile() function, which returns an Integer representing the next file number available for use by the Open statement.


The optional rangenumber argument is a Variant that specifies the range from which the next free file number is to be returned.

Closing A File

When you are finished with the open file, you must execute code that will close the file. Closing the file releases the file buffer and returns the file number to the unused pool. It also flushes the buffer so that all new data for the file is saved.

The Close statement concludes input/output (I/O) to a file opened using the Open statement.

Close [filenumberlist]

The following code segment opens two sequential files-one for reading and one for writing.

Dim intRFile As Integer, intWFile As Integer
intRFile = FreeFile
Open "read.txt" For Input As intRFile

intWFile = FreeFile
Open "write.txt" For Output As intWFile

' Code to read and write files goes here

Close intRFile
Close intWFile

© Universal Teacher Publications        INDEX Previous Screen Next Screen