What difference between module and class module?

A

a

access 2007

What difference between module and class module?

That created in access ribbon bar…

I mean

You choose create module

Or choose create class module
 
J

Jeff Boyce

Is this a homework question?

Try searching on-line for "class module".

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
A

Albert D. Kallal

A class module was introduced in access 97 I believe, and it is a simple
addition of MS access that allows you as a developer to create what is
called an object.

For example when you use a recordset in code you're using what's called an
object

eg:

set rstData = currentdb.OpenRecordSet(strSql)

The above would execute some SQL and place results into an object called
rstData. This object will have many methods( things that run code), and many
properties (things that useally return a value)

eg:

rstData.RecordCount

"RecordCount" is a property of this object that returns the number of
records (this is considered a property of the object).

rstData.Delete

"delete" is a method of this object, and methods generally perform some
action, (run code..do somthing)

So this object has variables in it, and also a bunch of related pieces of
code (functions) that you can run in that object. pass from all Spartan
audit is a mix of code in dated in one single object that you can use in
your code. your most common use of such objects in MS access is of course
the record set object as above.

The visual basic programming environment in MS access has the ability to for
YOU as a developer to create and use your own custom objects in code.

This is an affect your first steps down the road into learning object
oriented programming. While development and programming language in MS
access is not a full of object orientated development system, it does have
some features of object oriented programming, and one such feature is the
ability to create what is called class objects.

So, ms access is not a full OO system (OO programming is a common term used
in our industry, and it simply means all object oriented programming)

For more normal code writing in access, you'll always use and create what is
called a standard code module.
However if you want to create your own custom class object, then you'll
insert a class module.

I explain in detail here as to why you might want to use and create a class
object in MS access:

http://www.members.shaw.ca/AlbertKallal/Articles/WhyClass.html
 
J

Jesper F

Thanks for the explanation. I've always had trouble grasping where I'd have
the need to use a class module although I code extensively in standard
modules.
It seems I can often acheive the same things using series of functions and
functions taking results of other functions as parameters.

Are there memory/performance improvements when using classes?
Are things in the class module somehow loaded into memory when the app
starts?

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?

Thanks

/ Jesper
 
A

Albert D. Kallal

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).
 
J

Jesper F

Thanks Albert, really appreciate that. It's food for thought.
I think I'll attempt the FTP class :)

/ Jesper
 
J

Jesper F

The above is a great example for class object

Say in an ftp-object - how would you say the UploadFiles sub should probably
be created.
Something like

Public Sub UploadFiles(FilePaths As String,LocalPath as string, ServerPath
as string)
End Sub

or simply

Public Sub UploadFiles()
End Sub

with filepaths, localpaths, serverpaths, username, password and everthing
else needed to upload the files handled at setable properties? Is one more
correct than the other in this regard?
Since the mentioned values are not changing a lot of don't have to
calculated through compex logic the former approach may be just fine?

/ Jesper
 
J

Jamie Collins

It seems I can often acheive the same things using series of functions and
functions taking results of other functions as parameters.

Are there memory/performance improvements when using classes?

A 'class modules' approach in VBA6 typically requires better planning
and more work 'up front' than the equivalent 'traditional' (.bas
modules) approach. Such an approach in my experience tends to be of
interest only when you have a requirement to write code for other
developers to use in code. So when you expressed an interested in
"performance improvements", are you a designer looking to make things
easier on your coders?

Note that it seems to me that not many regulars who post replies in
the Access newsgroups seem to be in a 'coding for coders' situation;
also, many people who got interested in OOP in VBA6 have since moved
on (progressed?) to .NET. In other words, this may not be the best
place to get a balanced view on the subject.

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top