text file

emdiesse

Registered User.
Local time
Today, 16:12
Joined
Feb 1, 2006
Messages
36
How do I create a text file and write to the text file. I am trying to create a log.txt which will be used throughout my database to log activities taking place.

For example, if a user clicks a button (which has a piece of code in the on click event which writes a line to the text file) I want it to add the line specified in the code.

Please help. Say if you don't understand I will attempt to word it better.
Thanks, Emdiesse.
 
These are from the "FileSystemObject" category. You can determine drives, drive type, use dictionary...to see these go to a module and use the vba help. The following is one method from the help screen:

TextStream Object


Description

Facilitates sequential access to file.

Syntax

TextStream.{property | method}

The property and method arguments can be any of the properties and methods associated with the TextStream object. Note that in actual usage TextStream is replaced by a variable placeholder representing the TextStream object returned from the FileSystemObject.

Remarks

In the following code, a is the TextStream object returned by the CreateTextFile method on the FileSystemObject:

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close

WriteLine and Close are two methods of the TextStream Object.
 
Ok. I would like the log to be created when the user clicks create log in a certain form (this only needs to be done once). So the coden for the btnCreate log will need to be:

Code:
Sub CreateAfile
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\log.txt", True)
    a.WriteLine("Log created.")
    a.Close
End Sub

Once that has been created I would like it to be available round the whole system. So I am guessing I will need to open up the text file when each form is opened and to close it when the user closes the form? Or is there a way to have it open wehen the system(access database) is opened and stay open until the system (access database) is closed?

To open a text file to write to it (Using the form method) i would use:

Code:
Private Sub Form_Load()
Sub OpenTextFileLog()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim fs, l
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set l = fs.OpenTextFile("c:\log.txt", ForAppending, TristateFalse)
    l.Write "Menu form opened."
    l.Close
End Sub
End Sub

The on click code for each component would be:

Code:
Private Sub btnButton_Click()
Object.WriteLine ([Blah Blah Blah])
End Sub

Is this all right?

I would like the directory to be under the directory that the main program is being run from in a folder called system. but I'm not sure how to do this. Would I just type this ../system/log.txt instead of C:\log.txt

How do insert date and time stamps?

Instead of opening the text file on each form load, i would like it to be opened for appending when the database is loaded up and closed when the database is closed. How do I do this?

Thanks, emdiesse
 
Last edited:

Users who are viewing this thread

Back
Top Bottom