Need help to Understand Class Modules

H

Henry

Can anyone explain in simple layman's terms what Class modules
are and how they can be used? I have read numerous articles and
book chapters on the subject and still can't quite get my head around
the concept. I have a rudimentary knowledge of VBA and have successfully
designed several databases and still can't seem to figure out how &
where to use them.
PLEASE, please, don't refer me to any articles or books that use
heavy technical terminology(that's where I get lost)!
Are there ANY examples that might help explain the concept?

If this question offends you because it's asking for "Simple Layman's Terms"
please forgive me. I realize that this may be an unfair expectation,
but I am fairly descouraged at my lack of ability to comprehend the
explanations that are in any of the books/articles that I've read.

Thanks in advance for any feedback!
 
J

JonWayne

If you have ever used an object in VBA, say like a Collection, Database, or
RecordSet object - just to name a few - , then you should understand the
purpose of a class module. In procedures that you have written, and used
collection objects in , you must have seen where - when you are declaring
the collection object variable (Dim colSomething As Collection), as soon as
you enter the 'AS' clause of the declaration you get a list of available
data types. Then by the time you have entered 'Coll', the list scrolls down
to 'Collection', at which point you can hit <Enter> and VB inserts it for
you. Well, Collection, as well as all the other objects on that list, are
names of classes. Sombody before you took the time to insert a class module
into some VB dll, and wrote in that module a bunch of procedures, similar to
the procedures that you have written, that define the functionality of the
Collection object. Therefore when you later use colSomething in your codes,
after you have entered a period following the object variable name, you see
a list of methods and properties that the collection object exposes (Add,
Delete, Count, etc). The major difference with the procedures in this case
is that they are Property procedures (Property Let, Property Get, and
Property Set) which create the properties and methods of the object, instead
of Function and Sub procedures that you have used. They are also only
allowed in class modules.

So , in short, a class module is a module that is used to define an object.

If in your coding, you , say, frequently have the need to manipulate text
files, then instead of writing a new routine every time you want to
manipulate e text file, you could write a TextFile object. You can add to
your object, methods and properties that expose ways to do the things you
repeatedly have to do to text files. It may have a Find property that
accepts the path to a file you are interested in and returns an answer
telling whether the file exist ir not. It may also have a FileSize Property
that does just that, etc. TextFile could be the name of your class module
and that name will now exist on the list of data types within that or any
database that you copy that class module to.

Hope that did it for you
 
G

Graham R Seach

Henry,

In addition to JonWayne's reply, a class module is essentialy the definition
of an object. It is the same as a template.

Instantiating the object, using (as Jon suggests) Dim x As y, creates an
object using the class module as a template. In this case, the Dim x As y
syntax says "create an object called "x" which will be of the type "y".

Using class modules, you can create as many identical objects as you like,
each of which will be exactly the same in every way, except that the data
they contain (if any) will be completely separate and distinct from data in
any other object.

Creating class modules allows you to create "black boxes", that contain
everything it needs to control its own information and behaviour. For
example, creating a Dog class allows each Dog object to control its own
breed, colour, size, name, bark, temperament, and so on. But you only need
to create the code for the Dog class once - not many times for many
different dogs.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

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