It seems I can often acheive the same things using series of functions and
functions taking results of other functions as parameters.
The above is the preferred general approach. If you just have a couple
routines and functions then you can actually Group them into a single module
in MS access (as you likey already done).
The main reason to jump over to using a class module in *place* of a
standard when you have a group of routines realted is if you need multiple
copies of this in *memory*. Furthermore the class object really helps if all
the functions/subs need to operate (share) on a common (same) set of data
that is stored in variables.
Are there memory/performance improvements when using classes?
Are things in the class module somehow loaded into memory when the app
starts?
Really not much different than using most code. A class module is not loaded
as such, you have to create an instance just like when record set objects
are used
eg:
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset ' create an instance
rst.Open "select * from tblAnswers", CurrentProject.Connection
Note the use of the "new" keyword. if you did not want a use the new keyword
as above, THEN you could go:
Dim rst As new ADODB.Recordset
rst.Open "select * from tblAnswers", CurrentProject.Connection
When you use the "new" keyword in the variable definition as above, an
instance of the object is created when you **first** reference the object in
code. It is a preferred developers practice to NOT USE the "new" key word
when you define a variable. So, use the "new" in code. The reason for this
preference is there might be 5 places where the variable is referenced, but
at what point is the actual "first" instance of the actual object created?
Thus, if you use the new keyword in your code, then there's no ambiguity for
a person reading your code as to when you mean or intend to create that
instance of that object.
So, when you define a class object you can go :
dim myFTP as new clsFTP
or
dim myFTP as clsFTP
set myFTP = new clsFTP
note that when you create an instance of an object, there is an initialize
event procedure in your class object that gets called and run (this is an
optional routine that you don't have to place, just like the code for events
in a form are optional). so once again the supports the use of the new
keyword in code, and not and variable definition time, because you as the
developer want to control when the instances created. This is not a big
deal, but that's why you'll often see people tell you to avoid the use of
the new keyword at definition time.
For example - I have a large standard module that has a lot of FTP related
functions that automate use of windows ftp.exe
Some of the functions in this module are:
CreateDownloadScript (takes file list from local/server files and creates
script that downloads files from server)
CreateUploadScript (similar to the above)
CreateDeleteScript (similar to the above)
CreateRenameScript (similar to the above)
DeleteFTPfiles (deletes the scriptfiles that automate ftp.exe)
GetFileListFromLocal (does as it says)
GetFileListFromServer (does as it says)
RunFtpScript (runs script created above)
ZipFile (zips file)
Would you say that this should have been done with a class instead?
What would be other classic applications for class modules?
The above is a great example for class object for one main reason:
Once you set up connection and the URL to a particular server, then all of
the routines will be using a **common** set of values and variables for the
URL and connection stuff. If you just have one value and ten subroutines
that you have to pass the one value to those ten routines, then a class
object don't help. When you have a half dozen routines, and a half dozen
variables that all routines need to work on together, then actual grouping
of variables in code in a class object starts to make a lot of sense.
And of course if you make it a class object, then you could have two
different connections to two different servers open at the same time and
manipulate them in your code (and all of the variables between each object
would be local to each other, and not interfere with each other).
I suppose if you never needed more than one instance of FTP in your code,
then a class object would not show its real strength and benefits here. It
still would be likely easier to work a class object since you would not have
to define extra sets of variables to hold the URL etc. So not a big benefit
if never need more then one FTP, but it's still nice to have
values/variables etc. in one easy to use container (and you get Inteli sense
as you type in your code).