• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. Advanced VBA Techniques > Low-Level File Handling

Low-Level File Handling

On occasion, it is necessary to write data to, or read data from, a text file. This is often referred to as low-level file handling. Three types of file access exist: sequential, random, and binary. This text covers only sequential access. You use sequential access to read and write to a text file, such as an error log. You use the Open keyword to open a text file. You use the Input # keyword to read data. You use the Write # keyword to write data. Finally, you use the Close keyword to close the file. Here's an example:

Sub LogErrorText()
    Dim intFile As Integer

    'Store a free file handle into a variable
    intFile = FreeFile

    'Open a file named ErrorLog.txt in the current directory
    'using the file handle obtained above
    Open CurDir & "\ErrorLog.Txt" For Append Shared As intFile

    'Write the error information to the file
    Write #intFile, "LogErrorDemo", Now, Err, Error, CurrentUser()

    'Close the file
    Close intFile
End Sub


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint