Std Module vs Class Module

N

Newbie

Hi
Can somebody tell me when I should use a class module instead of a standard
module? and what the purpose of a Class module is

Thanks
 
J

JohnFol

A class is a representation of something. It has properties, can raise
events etc.
A module is a colloection of related functions.
 
T

TC

Newbie said:
Hi
Can somebody tell me when I should use a class module instead of a standard
module? and what the purpose of a Class module is
Thanks


A standard module contains procedures & functions that you can call from
anywhere, as long as they are declared as Public. For example, if a form
code module calls procedure BLAH, and BLAH is defined as a public procedure
in any standard module, that will call that procedure.

A class module defines a new "object" which can then be created witjh the
"New" keyword. The public procedures and functions defined in that module,
define the methods & properties of that object. For example, if you have a
class module named MYTHING, containing a public procedure MYSUB, you could
say:

dim temp as new MYTHING
temp.MYSUB

to invoke procedure MYSUB in that particular instance of MYTHING.

HTH,
TC
 
S

Scott McDaniel

Class modules tend to represent a real-world object (although this is not
always the case) ... you can also pass class objects around to various
function and procedures (something you can't do with Std modules) ... etc
etc. With a Class Module, you can have more than one object instantiated at
a time. For example, let's say you have a clsTire object with the following
Let/Get properties:

RimSize
TreadWidth
SideWallHeight
BrandName

With this method:
***************************************
Function FindTire(TireID As Long)

Dim rst As ADODB.Recordset

Set rst = New.ADODB.Recordset

rst.Open "SELECT * FROM tblTires WHERE lngTireID=" & TireID,
currentproject.connection

If Not rst.EOF and Not rst.BOF Then
RimSize = rst("strRimSize")
SideWallHeight = rst("strSideWallHeight")
TreadWidth = rst("strTreadWidth")
BrandName = rst("strBrandName")
End If

End Function
***************************************

If you wanted to compare two different tires sizes, you would instance two
separate tire objects:

Dim clsTire1 as clsTire
Dim clsTire2 As clsTire

Set clsTire1 = New clsTire
Set clsTire2 = New clsTire

clsTire1.FindTire 3
clsTire2.FindTire 5

At this point, clsTire1 contains info regarding a tire with the ID of 3, and
clsTire2 contains info regarding a tire with the ID of 5 ... something you
really can't do with Standard modules (without a LOT of Public variables,
which are not a great idea)

Class modules also allow you to include logic that represent other
characteristics of the object. For example, let's say you wanted to know
what types of rims you have in stock that will fit a particular tire ...
you could include a Method of your class that would report this info to you
merely by making a call to the class:

Me.lstListOfRims.ValueList = clsTire1.GetAvailableRims

This is just a very, very basic view of Class modules. For more information,
Google on "VB OOP", "class modules" etc etc
 
S

Sandra Daigle

In addition to the excellent information already given by others, I would
like to suggest these additional resources and references:

Class Modules - Simplify and Accelerate Your Development Processes
Class Act By Ken Getz and Mike Gilbert
http://www.microsoft.com/OfficeDev/Articles/classmod.htm

The Access(2000 or above) Developers Handbook - excellent discussion and
usable examples

For a few working examples take a look at

clsCtlGroups
http://www.daiglenet.com/MSAccess.htm

http://www.lebans.com/PrintLines.htm
 

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