HOW TO CREATE A TEXT FILE FROM MICROSOFT ACCESS

The FileSystemObject Object provides access to a the computer’s file system.

The following code illustrates how the FileSystemObject is used to return a TextStream object that can be read from or written to:

Syntax : Scripting.FileSystemObject

Example:

Sub CreateTextFile() 
Dim fs As Object, a  
On Error goto CreateTextFile_Err  
Set fs = CreateObject("Scripting.FileSystemObject") 
Set a = fs.CreateTextFile("C:\mytest.txt", True) 
a.writeline ("This is a test.") 
a.Close  

CreateTextFile_Exit: 
Exit Sub 

CreateTextFile_Err: 
Msgbox Err.Description,,"CreateTextFile" 
Resume CreateTextFile_Exit  
End Sub 

In the code shown above, the CreateObject function returns the FileSystemObject (fs). The CreateTextFile method then creates the file as a TextStream object (a), and the WriteLine method writes a line of text to the created text file. The Close method flushes the buffer and closes the file.

Reading Text File using FileSystemObject Example:

Sub ReadTextFile() 
Dim fs As Object, a, txtline  
On Error Goto ReadTextFile_Err  
Set fs = CreateObject("Scripting.FileSystemObject") 
Set a = fs.opentextfile("C:\mytest.txt") 
txtline = a.readline 
a.Close  
MsgBox "C:\mytest.txt File contents : " & txtline 
 
ReadTextFile_Exit: 
Exit Sub  

ReadTextFile_Err: 
Msgbox Err.Description,,"ReadTextFile" 
Resume ReadTextFile_Exit  
End Sub 

Next >> Rename File