By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

A simple question: What is the difference between a Module and a Class Module? (And Office 2003 Help system)

P: n/a
Hello everyone,

I have a newbe question:

In Access (2003) VBA, what is the difference between a Module and a
Class Module in the VBA development environment?

If I remember correctly, new types of objects (classes) can only be
defined in Class modules.

Am I correct?

Also, can you please tell me what is the keyword used to define a new
class?

If I have the keyword, I can probably be able to find the help in MS
Access's help program, but without it I cannot find it.

apropos the help system,
I think that Office 2003 help system is horrible, probably the worst I
ever seen. I almost cannot find anything I need, including things I
know their name. I just don't know how this help system works. For
example, this system does not recognize quotes: If I type "A B", it
considers it as A and B, and maybe A or B, I don't know.

I don't need explanations about it, but please tell me if you also it
is so horrible.

Thanks,

Jonathan Orlev

Jan 4 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
OK, I found something in the help system.

They say there that a Class module is used to define new classes, and
If I understand correctly, the name of the module is the name of the
new class.

But how do I change the default name of the class module, which is
'ClassN' (where N is a number), to something else?

Thanks,

Jonathan
JonathanOrlev wrote:
Hello everyone,

I have a newbe question:

In Access (2003) VBA, what is the difference between a Module and a
Class Module in the VBA development environment?

If I remember correctly, new types of objects (classes) can only be
defined in Class modules.

Am I correct?

Also, can you please tell me what is the keyword used to define a new
class?

If I have the keyword, I can probably be able to find the help in MS
Access's help program, but without it I cannot find it.

apropos the help system,
I think that Office 2003 help system is horrible, probably the worst I
ever seen. I almost cannot find anything I need, including things I
know their name. I just don't know how this help system works. For
example, this system does not recognize quotes: If I type "A B", it
considers it as A and B, and maybe A or B, I don't know.

I don't need explanations about it, but please tell me if you also it
is so horrible.

Thanks,

Jonathan Orlev
Jan 4 '07 #2

P: n/a
Well, I now see that when saving the DB, the program as for a name for
the module. So I chose an appropriate name, although I don't know if I
can re-name it without deleting it and creating a new one, but it does
make some sense I have to say.

Thanks,

Jonathan
JonathanOrlev wrote:
OK, I found something in the help system.

They say there that a Class module is used to define new classes, and
If I understand correctly, the name of the module is the name of the
new class.

But how do I change the default name of the class module, which is
'ClassN' (where N is a number), to something else?

Thanks,

Jonathan
JonathanOrlev wrote:
Hello everyone,

I have a newbe question:

In Access (2003) VBA, what is the difference between a Module and a
Class Module in the VBA development environment?

If I remember correctly, new types of objects (classes) can only be
defined in Class modules.

Am I correct?

Also, can you please tell me what is the keyword used to define a new
class?

If I have the keyword, I can probably be able to find the help in MS
Access's help program, but without it I cannot find it.

apropos the help system,
I think that Office 2003 help system is horrible, probably the worst I
ever seen. I almost cannot find anything I need, including things I
know their name. I just don't know how this help system works. For
example, this system does not recognize quotes: If I type "A B", it
considers it as A and B, and maybe A or B, I don't know.

I don't need explanations about it, but please tell me if you also it
is so horrible.

Thanks,

Jonathan Orlev
Jan 4 '07 #3

P: n/a
Glad we could help. ;o)

JonathanOrlev wrote:
Well, I now see that when saving the DB, the program as for a name for
the module. So I chose an appropriate name, although I don't know if I
can re-name it without deleting it and creating a new one, but it does
make some sense I have to say.

Thanks,

Jonathan
Jan 4 '07 #4

P: n/a
Rightly or wrongly I think about it like this, A form is a class, or
rather derived from a class.

So when you write your own class, although it looks like it, it's not a
function, it is like a "plan" , "a map" or maybe "a list" of
instructions like in the case of the form class, the Form class
controls the building of the form.

Look at it another way ---
When you add controls to a Form, text boxes, combo boxes etc, you are
creating a class in its own right, you can actually duplicate your
form, (not copy it) in the sense that you can open another instance of
it, or even a hundred instances of it. They all will be exactly the
same as the original form you built.

Each task you perform in the database is normally accessed through a
form. In effect you are designing a class to handle a particular
function.

And that's when you can write your own class when you identify a
specific function, a repetitive function in your database, why write
separate code to do it ? Put all the code into a class module and call
the class.

Jan 4 '07 #5

P: n/a
ThanQ Gizmo for the information. I will have to read it again to fully
understand, but I just want to show you that I read it.

Regards,

Jonathan

Uncle Gizmo wrote:
Rightly or wrongly I think about it like this, A form is a class, or
rather derived from a class.

So when you write your own class, although it looks like it, it's not a
function, it is like a "plan" , "a map" or maybe "a list" of
instructions like in the case of the form class, the Form class
controls the building of the form.

Look at it another way ---
When you add controls to a Form, text boxes, combo boxes etc, you are
creating a class in its own right, you can actually duplicate your
form, (not copy it) in the sense that you can open another instance of
it, or even a hundred instances of it. They all will be exactly the
same as the original form you built.

Each task you perform in the database is normally accessed through a
form. In effect you are designing a class to handle a particular
function.

And that's when you can write your own class when you identify a
specific function, a repetitive function in your database, why write
separate code to do it ? Put all the code into a class module and call
the class.
Jan 7 '07 #6

P: n/a
To rename a class:-
1) open the VBA IDE
(Alt-F11 on the keyboard)
2) Click on the class in Project Explorer
(Ctrl-R on the keyboard if Project Explorer isn't open)
3) Double Click on the class name in the properties window
(F4 on the keyboard if Properties isn't open)

Having said that you really should decide on a class name when you first
create it and stick with that.

The difference between a Module an a Class module.

A module holds code which is global to the project. The code in a module is
frequently a number of functions which are relatively unrelated.

A class module is a code template which represents something. The code in a
class module is all related and should be encapsulated (i.e. relies only on
itself) although this is not by any stretch of the imagination always
possible.

To show the differences have a look at
http://www.mvps.org/access/api/api0001.htm which shows how to use the
Open/Save File dialog in a standard module.

Personally I prefer to use a class module for this as it matches more easily
with the common dialog control and allows me to more easily convert apps
where that control has ben used, also thinking of the Open/Save dialog as a
form or object is more intuitive to me. So the code for a class module
tohandle thiscoule look like this.

' ******* Code Start ********
Option Explicit

'Code Courtesy of
'Terry Kreft

Private Type OPENFILENAME
lStructSize As Long
hWnd As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Declare Function GetOpenFileName Lib _
"comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib _
"comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_SAVE = 0
Private Const OFN_OPEN = 1

Private lngHwnd As Long
Private wMode As Integer
Private szDialogTitle As String
Private szFileName As String
Private szFilter As String
Private szDefDir As String
Private szDefExt As String
Private szFileTitle As String
Private szFileDir As String
Private intFilterIndex As Integer
Public Function Action() As String
Dim x As Long, OFN As OPENFILENAME
Call SetDefs
With OFN
.lStructSize = Len(OFN)
.hWnd = lngHwnd
.lpstrTitle = szDialogTitle
.lpstrFile = szFileName & String$(250 - Len(szFileName), 0)
.nMaxFile = 255
.lpstrFileTitle = String$(255, 0)
.nMaxFileTitle = 255
.lpstrFilter = szFilter
.nFilterIndex = intFilterIndex
.lpstrInitialDir = szDefDir
.lpstrDefExt = szDefExt
If wMode = 1 Then
OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
x = GetOpenFileName(OFN)
Else
OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or
OFN_PATHMUSTEXIST
x = GetSaveFileName(OFN)
End If
If x <0 Then
If InStr(.lpstrFile, Chr$(0)) 0 Then
szFileName = Left$(.lpstrFile, InStr(.lpstrFile, Chr$(0)) - 1)
szFileTitle = Left$(.lpstrFileTitle, InStr(.lpstrFileTitle,
Chr$(0)) - 1)
Call getFile_Dir
End If
Else
szFileName = ""
End If
End With
Action = szFileName
End Function
'Pass a bar separated string and returns a Null separated string
Private Function NullSepString(ByVal BarString As String) As String
Dim intInstr As Integer
Const vbBar = "|"
Do
intInstr = InStr(BarString, vbBar)
If intInstr 0 Then Mid(BarString, intInstr, 1) = vbNullChar
Loop While intInstr 0
NullSepString = BarString
End Function
Private Sub getFile_Dir()
Dim intInstr As Integer
intInstr = InStr(szFileName, szFileTitle) - 1
szFileDir = Left(szFileName, intInstr)
End Sub

Property Let hWnd(SourceHwnd As Long)
lngHwnd = SourceHwnd
End Property

Property Let ModeOpen(DialogMode As Boolean)
wMode = DialogMode * -1
End Property
Property Let Title(DialogTitle As String)
szDialogTitle = DialogTitle
End Property
Property Let FileName(DefaultFile As String)
szFileName = DefaultFile
End Property
Property Get FileName() As String
FileName = szFileName
End Property
Property Let Filter(FilterList As String)
szFilter = NullSepString(FilterList)
End Property
Property Let StartDir(InitialDir As String)
szDefDir = InitialDir
End Property
Property Let DefaultExtension(DefExt As String)
szDefExt = DefExt
End Property
Property Get FileTitle()
FileTitle = szFileTitle
End Property
Property Get FileDir() As String
FileDir = szFileDir
End Property
Private Sub SetDefs()
If lngHwnd = 0 Then lngHwnd = 0
If szFilter = "" Then szFilter = NullSepString("All Files|*.*")
If szDefDir = "" Then szDefDir = "C:\"
If intFilterIndex = 0 Then intFilterIndex = 1
End Sub
' ******* Code End ********

If we called this class cDialog then the code to use it could be as follows

' ******* Code Start ********
Dim cD As cDialog
Dim strFileName As String

Set cD = New cDialog

With cD
.Filter = "Excel Files|*.xls|All Files|*.*"
.StartDir = "c:\"
.Title = "Find Excel File ..."
.ModeOpen = True
strFileName = .Action
End With
Set cD = Nothing

Debug.Print strFileName

' ******* Code End ********
--

Terry Kreft
"JonathanOrlev" <Jo***********@gmail.comwrote in message
news:11**********************@i15g2000cwa.googlegr oups.com...
Hello everyone,

I have a newbe question:

In Access (2003) VBA, what is the difference between a Module and a
Class Module in the VBA development environment?

If I remember correctly, new types of objects (classes) can only be
defined in Class modules.

Am I correct?

Also, can you please tell me what is the keyword used to define a new
class?

If I have the keyword, I can probably be able to find the help in MS
Access's help program, but without it I cannot find it.

apropos the help system,
I think that Office 2003 help system is horrible, probably the worst I
ever seen. I almost cannot find anything I need, including things I
know their name. I just don't know how this help system works. For
example, this system does not recognize quotes: If I type "A B", it
considers it as A and B, and maybe A or B, I don't know.

I don't need explanations about it, but please tell me if you also it
is so horrible.

Thanks,

Jonathan Orlev

Jan 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.