Classes vs modules

  • Thread starter Thread starter Chris Nebinger
  • Start date Start date
C

Chris Nebinger

Classes and modules are totally seperate beasts.

Modules contain functions and subroutines. You can call
them directly (Date(), DateAdd(), Replace(), Split()).

Classes are objects. They have their own properties,
methods, and events. DAO is full of objects (Database,
Recordset, TableDef, QueryDef). You have to create
objects, then change properties or call methods.

A good resource for using class modules in Access is here:

http://www.attcanada.net/%
7ekallal.msn/Articles/WhyClass.html

To add to that, I also use classes whenever I have complex
code that I will move from one database to the other. I
have a class that wraps the API calls to bring up the File
Open dialog box.

Also, if you have many developers working on a project,
you can keep code seperate from others. Also, you can
write a class to return dummy data, and then do
development around that class until you can finish and
test the entire class.



Chris Nebinger

-----Original Message-----
Hey all,

I'm using Access 2000, what's the difference between
classes and modules? Does it have anything to do with one
has to be instantiated and the other not?
 
ari said:
Chris thanks,

There was code that I had a question about:

Dim Students As Collection
Set Students = New Collection

Dim oNewStudent As Student

Set oNewStudent = New Student

With oNewStudent
.FirstName = "Bart"
.LastName = "Simmons"
.Birthdate = #6/6/1987#
End With
Students.Add oNewStudent, oNewStudent.FirstName

With oNewStudent
.FirstName = "Lisa"
.LastName = "Simmons"
.Birthdate = #5/1/1991#
End With
Students.Add oNewStudent, oNewStudent.FirstName

MsgBox Students.Count


The count shows 2 but when I try to access each item I can only get to the
last item added to the collection. Any ideas?


I believe if you need to
Set oNewStudent = New Student
each time you want to add a new student object to the collection.

If you don't you are changing the existing object that oNewStudent is
refering to.
 
I suggest you pick up the Access Developer's Handbook.
One of the beginning chapters has good information about
creating classes.

Essentially, what you want to do is a collection class.

Save the following as a text file, then import it into the
VBE.

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Tables"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit

Private mcol As Collection

Private Sub Class_Initialize()
Set mcol = New Collection
End Sub

Private Sub Class_Terminate()
Set mcol = Nothing
End Sub
Property Get Count() As Integer
Count = mcol.Count
End Property
Public Sub Add(S As Student)
mcol.Add S, S.Name
End Sub
Public Sub Remove(strSudent As String)
mcol.Remove strSudent
End Sub
Public Function Item(Index) As Student
Attribute NewEnum.VB_UserMemId = 0
Set Item = mcol(Index)
End Function
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = mcol.[_NewEnum]
End Function




Now you can:


Dim Students as New Students
Dim Student as New Student
With Student
.FirstName = "Bart"
.LastName = "Simpson"
.Key = "BS001"
.Birthdate = #5/1/1991#
End With

Students.Add Student
Set Student = New Student
With Student
.FirstName = "Lisa"
.LastName = "Simpson"
.Key = "BS002"
.Birthdate = #5/1/1991#
End With

For Each Student in Students
Debug.print Student.FirstName & " " & Student.LastName
Next Student



Chris Nebinger
 
Chris Nebinger said:
I suggest you pick up the Access Developer's Handbook.
One of the beginning chapters has good information about
creating classes.

Essentially, what you want to do is a collection class.

Save the following as a text file, then import it into the
VBE.

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Tables"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit

Private mcol As Collection

Private Sub Class_Initialize()
Set mcol = New Collection
End Sub

Private Sub Class_Terminate()
Set mcol = Nothing
End Sub
Property Get Count() As Integer
Count = mcol.Count
End Property
Public Sub Add(S As Student)
mcol.Add S, S.Name
End Sub
Public Sub Remove(strSudent As String)
mcol.Remove strSudent
End Sub
Public Function Item(Index) As Student
Attribute NewEnum.VB_UserMemId = 0
Set Item = mcol(Index)
End Function
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = mcol.[_NewEnum]
End Function




Now you can:


Dim Students as New Students
Dim Student as New Student
With Student
.FirstName = "Bart"
.LastName = "Simpson"
.Key = "BS001"
.Birthdate = #5/1/1991#
End With

Students.Add Student
Set Student = New Student
With Student
.FirstName = "Lisa"
.LastName = "Simpson"
.Key = "BS002"
.Birthdate = #5/1/1991#
End With

For Each Student in Students
Debug.print Student.FirstName & " " & Student.LastName
Next Student

You don't need any of your collection class code in order to execute the
code after your "Now you can:" line.

At least not in Access 2002. Is all that required in A97 or something?
 
Really? You can do a "For each Student in Students" loop without the
collection class? You sure about that?

I think you will find that you can do "For i = 1 to Students.Count" but not
a "For each".

:-)

George Nicholson

--
George Nicholson

Remove 'Junk' from return address.



rkc said:
Chris Nebinger said:
I suggest you pick up the Access Developer's Handbook.
One of the beginning chapters has good information about
creating classes.

Essentially, what you want to do is a collection class.

Save the following as a text file, then import it into the
VBE.

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Tables"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit

Private mcol As Collection

Private Sub Class_Initialize()
Set mcol = New Collection
End Sub

Private Sub Class_Terminate()
Set mcol = Nothing
End Sub
Property Get Count() As Integer
Count = mcol.Count
End Property
Public Sub Add(S As Student)
mcol.Add S, S.Name
End Sub
Public Sub Remove(strSudent As String)
mcol.Remove strSudent
End Sub
Public Function Item(Index) As Student
Attribute NewEnum.VB_UserMemId = 0
Set Item = mcol(Index)
End Function
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = mcol.[_NewEnum]
End Function




Now you can:


Dim Students as New Students
Dim Student as New Student
With Student
.FirstName = "Bart"
.LastName = "Simpson"
.Key = "BS001"
.Birthdate = #5/1/1991#
End With

Students.Add Student
Set Student = New Student
With Student
.FirstName = "Lisa"
.LastName = "Simpson"
.Key = "BS002"
.Birthdate = #5/1/1991#
End With

For Each Student in Students
Debug.print Student.FirstName & " " & Student.LastName
Next Student

You don't need any of your collection class code in order to execute the
code after your "Now you can:" line.

At least not in Access 2002. Is all that required in A97 or something?
 
George Nicholson said:
Really? You can do a "For each Student in Students" loop without the
collection class? You sure about that?

With the standard VBA.Collection class.
Without all the wrapper code that was posted.
I understand the 'custom' collection class that was posted ensures that
only student objects are added to the contained private collection.

I was really questioning the purpose of this:
Attribute NewEnum.VB_UserMemId = 0
Set Item = mcol(Index)
End Function
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Set NewEnum = mcol.[_NewEnum]
End Function
 
Back
Top