[AccessD] Log Class part 1A

JWColby jwcolby at colbyconsulting.com
Sat Jan 6 11:53:59 CST 2007


I thought you guys might appreciate seeing how I do this.  
 
I need to log things to text files all of the time for exporting data out to
clients.  I am writing processes (classes) where I export data to fixed
width files.  The export process is table driven and allows taking a
specification from the client that says what fields are expected, how long
each field is, how they are padded (left / right), with what characters
(space, zeros etc), date formats and so forth.  That export process (class
system) performs an export using a query that I build to get the data ready
for export.  The end result of that class system is a sequence of text
strings that are the export data.  Now a common task is that these export
systems need to write the data out to disk.  Another common task is that the
export process needs to log any errors that it encounters.
 
Given that this is a repetitive task I decided to create a class that my
export process classes could instantiate and then use.  It uses the Windows
File System Object, so if you work under a notwork Nazi who has locked down
your systems, you can pretty much stop reading now, or... you might want to
replicate this using the methods for file manipulations built in to Access.
I like the FSO and use it where possible.
 
This log class is designed to be instantiated, then left open to write (or
read)  to a text file.  In the Init() I pass in the pieces of the file name,
the path, the file name, the extension.  I like to date / time stamp my
files in the file name so that I can write the same type of file out to a
common directory and be able to see right in the file name the date and time
that the file was written.  So I also allow passing in a date format string
and a time format string.  If you are going to only do this once a week and
don't care about the time, then only pass in the date format.  If you are
going to do this several times in a day, then also pass in a time format.
 
The init function builds up a file spec which is the fully pathed location
of the file.  ATM it does not attempt to create the directory path to get
there, although eventually it will do that, so that if the path does not
exist it will attempt to build the path (directory structure).  
 
The log class has methods for recomputing the file spec, for example if the
time has changed.  There are a couple of simple methods for getting a read
or write text stream.  There are also properties for reading and writing the
file spec pieces individually if desired.  And finally there are two
properties to allow getting a pointer to the file system object, and the
text stream object.  Using the pointer to the text stream object, you can
then directly write to the log file.  Since the class holds the text stream
open until the class unloads or you intentionally close it, you can load
this class and just start writing to the text file.  Close when you are
done.
 
This class is an example of wrapping another object in order to extend that
object's functionality with your own.  As you know VBA does not directly
allow inheritance, however by using wrapper classes like this you can
crudely emulate inheritance.  I add functionality to the text stream object
here.  
 
To use the class you need to dim and instantiate the log class:
 
dim clsMyLog as clsLogFile
    set clsMyLog = new clsLogFile
    clsMyLog.Init(mclsLog.Init "C:\Dev\DISNEW\", "Test", "LOG", "YYYYMMDD",
"HHMMSS")
 
Now you are ready to write to the file.  Until such time as you unload the
class instance you can write to the text stream.
        
    clsMyLog.pTS.WriteLine "test"
 
clsMyLog gets a pointer to the log file class, pTS gets a pointer to the
open text stream, .Writeline calls the method of the textstream, and "test"
is written to the file.
 
The file name will look something like Test-20070106-105937.LOG and will be
located in the path C:\Dev\DISNEW\.
 
Throw this class out in your library, expose it, and use it as needed.
 
Part 1B to follow
 
John W. Colby
Colby Consulting
www.ColbyConsulting.com
 



More information about the AccessD mailing list