File I/O Using VBA
As well as these functions, you may also find that you will need to use some of the VBA string handling functions, such as InStr, Left, Mid, Right and Trim, to parse the data as required.
There is an example of some of these functions on this page being used to read data in from the cdplayer.ini file.
EOFThis function indicates when you have reached the end of a file, and as such it is normally used as the condition for a Do loop.
Input #This function will read data from a file that has been written to using the Write function. As this means that text will normally be delimited with quotes and similar, it is not of much use for general text importing.
InputThe Input function will import the number of specified characters at a time into Access. This is probably of most use when you have a record of fixed width (i.e. you have set the Len argument of the Open statement). One trick with this function is to read the whole file into Access in one go - this is often faster than reading it line by line or character by character, and then separating it:
Line InputThis function will input a line at a time from the text file. You will often read this into a temporary variable, and then split it using the string handling functions (Left, Mid, etc.) as required.
WriteThis function is used to write data to a file, but as it 'wraps' data with special characters (such as double quotes) it is not that useful as far as exporting data for use in other programs is concerned. Also be aware that it ignores locale settings, such as the decimal separator.
CloseYou use this function to close a file that has been opened using the Open statement. It is important to close files that you have opened, either using this function or Reset, otherwise they are 'locked'.
ResetYou use this function to reset all files that have been opened - therefore it should be used with care, as you may find yourself closing a file that has been opened in another procedure. Unless I know that I have a file open in another procedure, I will normally have this statement in the exit point for a procedure, otherwise I will use the Close statement on each individual file.
Copyright & Disclaimer
Last modified at 06/06/2006 14:58:05