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

Custom Classes

P: n/a
I've been trying to get my head around custom classes by following the
example in the Visual Basic Language Developer's Handbook by Sybex. I think
I have a handle on what they're about, albeit a fragile one at the moment.
Having said that, I can't think of a single application for one so obviously
I need educating further. Does anyone have any practical examples of custom
classes and their application that might help me broaden my horizons?

Many thanks.

Keith.
Nov 30 '06 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Anything can be a custom class. Think of anything that has multiple
variables and you can make it into a custom class.

Take an Employee for example. I am an employee, you presumably are an
employee.

At the root, all employees have a first name, last name, birthdate,
SSN, etc. You could store each of those variables and have a
JasonLepack as Employee, or a KeithWilby as Employee.

Another example is a slot machine. Each slot machine has a few
variables.
It has a cost structure, payout structure, amount of money in it, data
on the reels, current position of reels.

On top of that a slot machine has actions (functions) that can be
performed. You could depositMoney, pullArm, fullPlay (the button on
the front), payOut.

Hope that makes a little more sense.

Cheers,
Jason Lepack

Keith Wilby wrote:
I've been trying to get my head around custom classes by following the
example in the Visual Basic Language Developer's Handbook by Sybex. I think
I have a handle on what they're about, albeit a fragile one at the moment.
Having said that, I can't think of a single application for one so obviously
I need educating further. Does anyone have any practical examples of custom
classes and their application that might help me broaden my horizons?

Many thanks.

Keith.
Nov 30 '06 #2

P: n/a
Keith Wilby wrote:
I've been trying to get my head around custom classes by following the
example in the Visual Basic Language Developer's Handbook by Sybex. I
think I have a handle on what they're about, albeit a fragile one
at the moment. Having said that, I can't think of a single
application for one so obviously I need educating further. Does
anyone have any practical examples of custom classes and their
application that might help me broaden my horizons?
Many thanks.

Keith.
I have classes that ...

Provides the Windows File Browser dialog
Creates and sends an Email using Outlook
Creates and send an Email using CDO
Displays a custom Message Dialog Form
(others that are to esoteric to bother describing)

There are times when having a bunch of related code in a class is better
than just having functions and sub-routines in a module. Classes can expose
certain things while not exposing others. You can instantiate multiple
objects of a Class at the same time. Since they are instantiated as objects
you can pass them around between other functions and sub-routines.

I'm not an expert at using them in Access and Classes in VBA have some
limitations, but they definitely have their place. Some developers uses
classes more than others especially if they came from a programming
background where object-oriented was stressed.

It is good to learn about Classes and be able to dig into those you
encounter and see what's going on, but don't feel guilty if you can't think
of hundreds of ways to use them. In most cases plain old functions and
sub-routines solve the problem in the best fashion.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com

Nov 30 '06 #3

P: n/a

Keith Wilby wrote:
I've been trying to get my head around custom classes by following the
example in the Visual Basic Language Developer's Handbook by Sybex. I think
I have a handle on what they're about, albeit a fragile one at the moment.
Having said that, I can't think of a single application for one so obviously
I need educating further. Does anyone have any practical examples of custom
classes and their application that might help me broaden my horizons?
There aren't a lot of uses for classes. The major one is to make
programers/developers feel that they have arrived.
I use my own classes almost never but over the years I have defended
them as useful for:
1. standardizing procedures in an application developed by many
persons; for example an append class;
2. allowing for multiple instances of code to exist concurrently; for
example code that persisted values while forms were closed and opened;
3. code which used objects or properties whose creation and or
initialization were time consuming; for example a numerals to words
procedure where arrays and strings were used.

You have used classes extensively if you have used forms that have
modules, and I expect that you have. The form module is, for all
extents and purposes, a class module. It has initalization and
termination procedures (form_open and form_close), properties in the
form of controls or property definitions, and can have multiple
instances. IMO the form is a very superior Class. When invisible it
acts just like a class. It can be made visible on demand (and invisible
again on demand) to get input from the user, to show as progress bar,
to show a message, etc and as a single default entity it does not have
to be initialized; we can jst call Form_MYClass.DoSomething()
[DoSomething is a Public Procedure] and slam, bam it's done.

Nov 30 '06 #4

P: n/a
"Rick Brandt" <ri*********@hotmail.comwrote in message
news:sw****************@newssvr27.news.prodigy.net ...
>
It is good to learn about Classes and be able to dig into those you
encounter and see what's going on, but don't feel guilty if you can't
think of hundreds of ways to use them. In most cases plain old functions
and sub-routines solve the problem in the best fashion.
You've hit the nail right on the head there Rick because I do extensively
use subs/functions to return values and perform various tasks, I suppose I'm
going to have to keep reading up until the penny fully drops to see if any
would be more efficient as a class. Thanks for your response.

Keith.
Nov 30 '06 #5

P: n/a
"jlepack" <jl*****@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
Anything can be a custom class. Think of anything that has multiple
variables and you can make it into a custom class.

Take an Employee for example. I am an employee, you presumably are an
employee.

At the root, all employees have a first name, last name, birthdate,
SSN, etc. You could store each of those variables and have a
JasonLepack as Employee, or a KeithWilby as Employee.
<snip>

Thanks Jason, I'll have a go at creating one of those, should help that
penny drop.

Regards,
Keith.
Nov 30 '06 #6

P: n/a
"Lyle Fairfield" <ly***********@aim.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
<snip>

Thanks Lyle.
Nov 30 '06 #7

P: n/a
Keith Wilby wrote:
"jlepack" <jl*****@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
>Anything can be a custom class. Think of anything that has multiple
variables and you can make it into a custom class.

Take an Employee for example. I am an employee, you presumably are
an employee.

At the root, all employees have a first name, last name, birthdate,
SSN, etc. You could store each of those variables and have a
JasonLepack as Employee, or a KeithWilby as Employee.
<snip>

Thanks Jason, I'll have a go at creating one of those, should help
that penny drop.

Regards,
Keith.
An easy example where *using* a class can be easier. Take my Email class.
As a class it works something like this.

Dim msg as New CDOMsg

With msg
.AddRecipient("SomeAddress")
.Subject = "foo"
.TextBody = "bar"
.Send
End With

Now, that class has lots of other properties and methods that I might use
from time to time, but most often I only use the ones you see above. If I
wrote that as a standard function I would have to write it out with a bunch
of comma separated arguments and include all the commas for the arguments I
don't use and all optional arguments would have to be specified at the end
of the function signature. I could use the named argument syntax with a
standard function and "almost" do it the same as with the class, but its
just nicer to be able to use the class syntax.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com

Nov 30 '06 #8

P: n/a
"jlepack" <jl*****@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
>
Take an Employee for example. I am an employee, you presumably are an
employee.

At the root, all employees have a first name, last name, birthdate,
SSN, etc. You could store each of those variables and have a
JasonLepack as Employee, or a KeithWilby as Employee.
Any chance of posting an example Jason?

Keith.
Nov 30 '06 #9

P: n/a
"Rick Brandt" <ri*********@hotmail.comwrote in message
news:JS****************@newssvr27.news.prodigy.net ...
>
An easy example where *using* a class can be easier. Take my Email class.
As a class it works something like this.

Dim msg as New CDOMsg

With msg
.AddRecipient("SomeAddress")
.Subject = "foo"
.TextBody = "bar"
.Send
End With
<snip>

Many thanks Rick. I think I'm OK using the class, it's what's in the class
itself that my small brain is struggling with. Here's what I have in my
Employee class so far:

Private mstrLastName As String
Private mstrFirstName As String
Private mdteDOB As Date
Private mintEmployeeNo As Integer

Property Get LastName()
LastName = mstrLastName
End Property

Property Get FirstName()
FirstName = mstrFirstName
End Property

Now it's the properties that are stumping me here because when I create a
new instance of Employee I want to assign values:

Dim objEmployee As clsEmployee
Set objEmployee = New clsEmployee
objEmployee.LastName = "Wilby"
objEmployee.FirstName = "Keith"

but of course this doesn't work. I'm not sure what's happening in those
property declarations. The logical part of my brain has quit for Christmas
already I think.

Keith.
Nov 30 '06 #10

P: n/a

Keith Wilby wrote:
"Rick Brandt" <ri*********@hotmail.comwrote in message
news:JS****************@newssvr27.news.prodigy.net ...

An easy example where *using* a class can be easier. Take my Email class.
As a class it works something like this.

Dim msg as New CDOMsg

With msg
.AddRecipient("SomeAddress")
.Subject = "foo"
.TextBody = "bar"
.Send
End With
<snip>

Many thanks Rick. I think I'm OK using the class, it's what's in the class
itself that my small brain is struggling with. Here's what I have in my
Employee class so far:

Private mstrLastName As String
Private mstrFirstName As String
Private mdteDOB As Date
Private mintEmployeeNo As Integer

Property Get LastName()
LastName = mstrLastName
End Property

Property Get FirstName()
FirstName = mstrFirstName
End Property

Now it's the properties that are stumping me here because when I create a
new instance of Employee I want to assign values:

Dim objEmployee As clsEmployee
Set objEmployee = New clsEmployee
objEmployee.LastName = "Wilby"
objEmployee.FirstName = "Keith"

but of course this doesn't work. I'm not sure what's happening in those
property declarations.
The Property Get statements allow you to retrieve the value of the
named property. A Property Let statement will enable you to give the
property a value. I have never been a "classy" programmer , but I
believe the magic you seek is

Property Let LastName(ByVal vNewValue As Variant)
mstrLastName = vNewValue
End Property

(I'm sure that someone will correct me if I am wrong.)

Nov 30 '06 #11

P: n/a
"Gord" <gd*@kingston.netwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>

The Property Get statements allow you to retrieve the value of the
named property. A Property Let statement will enable you to give the
property a value. I have never been a "classy" programmer , but I
believe the magic you seek is

Property Let LastName(ByVal vNewValue As Variant)
mstrLastName = vNewValue
End Property

(I'm sure that someone will correct me if I am wrong.)
Clearly I have much to learn still. Many thanks for that pointer Gord, I
will now go back to my book!

Regards,
Keith.
Nov 30 '06 #12

P: n/a
Keith,

Your problem here is that oyu have followed sound programming
principles. You have created your variables within your class as
Private. Therefore, only methods and functions within your class can
access them, same as a private variable anywhere else for that matter.
As Gord correctly noted, these Get and Let methods can be accessed
publicly and therefore will allow non-local functions and procedures
that have an instance of your class to modify/view those values.

PS. You took the "unintersting path" and chose the employee and not
the slot machine ;)

Cheers,
Jason Lepack

Keith Wilby wrote:
"Rick Brandt" <ri*********@hotmail.comwrote in message
news:JS****************@newssvr27.news.prodigy.net ...

An easy example where *using* a class can be easier. Take my Email class.
As a class it works something like this.

Dim msg as New CDOMsg

With msg
.AddRecipient("SomeAddress")
.Subject = "foo"
.TextBody = "bar"
.Send
End With
<snip>

Many thanks Rick. I think I'm OK using the class, it's what's in the class
itself that my small brain is struggling with. Here's what I have in my
Employee class so far:

Private mstrLastName As String
Private mstrFirstName As String
Private mdteDOB As Date
Private mintEmployeeNo As Integer

Property Get LastName()
LastName = mstrLastName
End Property

Property Get FirstName()
FirstName = mstrFirstName
End Property

Now it's the properties that are stumping me here because when I create a
new instance of Employee I want to assign values:

Dim objEmployee As clsEmployee
Set objEmployee = New clsEmployee
objEmployee.LastName = "Wilby"
objEmployee.FirstName = "Keith"

but of course this doesn't work. I'm not sure what's happening in those
property declarations. The logical part of my brain has quit for Christmas
already I think.

Keith.
Nov 30 '06 #13

P: n/a
Keith Wilby wrote:
"Gord" <gd*@kingston.netwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>>

The Property Get statements allow you to retrieve the value of the
named property. A Property Let statement will enable you to give the
property a value. I have never been a "classy" programmer , but I
believe the magic you seek is

Property Let LastName(ByVal vNewValue As Variant)
mstrLastName = vNewValue
End Property

(I'm sure that someone will correct me if I am wrong.)

Clearly I have much to learn still. Many thanks for that pointer
Gord, I will now go back to my book!

Regards,
Keith.
The problem is you made those properties Private. Private properties are
either intended to be used strictly within the class or must have a Let (or
Set) function that can be used by code outside the class.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com

Nov 30 '06 #14

P: n/a
Lyle Fairfield wrote:
Keith Wilby wrote:
I've been trying to get my head around custom classes by following the
example in the Visual Basic Language Developer's Handbook by Sybex. I think
I have a handle on what they're about, albeit a fragile one at the moment.
Having said that, I can't think of a single application for one so obviously
I need educating further. Does anyone have any practical examples of custom
classes and their application that might help me broaden my horizons?

There aren't a lot of uses for classes. The major one is to make
programers/developers feel that they have arrived.
I use my own classes almost never but over the years I have defended
them as useful for:
1. standardizing procedures in an application developed by many
persons; for example an append class;
2. allowing for multiple instances of code to exist concurrently; for
example code that persisted values while forms were closed and opened;
3. code which used objects or properties whose creation and or
initialization were time consuming; for example a numerals to words
procedure where arrays and strings were used.

You have used classes extensively if you have used forms that have
modules, and I expect that you have. The form module is, for all
extents [:-)] and purposes, a class module. It has initalization and
termination procedures (form_open and form_close), properties in the
form of controls or property definitions, and can have multiple
instances. IMO the form is a very superior Class. When invisible it
acts just like a class. It can be made visible on demand (and invisible
again on demand) to get input from the user, to show as progress bar,
to show a message, etc and as a single default entity it does not have
to be initialized; we can jst call Form_MYClass.DoSomething()
[DoSomething is a Public Procedure] and slam, bam it's done.
Slam, bam, thank-you RAM? Is that from a call to
Form_ClassicRock.GetAlteredLyricSnippet()?

James A. Fortune
CD********@FortuneJames.com

Nov 30 '06 #15

P: n/a
I give some thoughts and helpfull advice here:
Using Class objects with ms-access.
By Albert D. Kallal
Tuesday, September 16, 2003

Why would I want to use a class object in ms-access?

http://www.members.shaw.ca/AlbertKal.../WhyClass.html
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com

Nov 30 '06 #16

P: n/a
"Albert D. Kallal" <Pl*******************@msn.comwrote in message
news:GkHbh.398409$5R2.297739@pd7urf3no...
>I give some thoughts and helpfull advice here:
Using Class objects with ms-access.
By Albert D. Kallal
Tuesday, September 16, 2003

Why would I want to use a class object in ms-access?

http://www.members.shaw.ca/AlbertKal.../WhyClass.html
Thank you Albert, I will read that now.
Dec 1 '06 #17

P: n/a
"jlepack" <jl*****@gmail.comwrote in message
news:11**********************@l39g2000cwd.googlegr oups.com...
Keith,

Your problem here is that oyu have followed sound programming
principles. You have created your variables within your class as
Private. Therefore, only methods and functions within your class can
access them, same as a private variable anywhere else for that matter.
As Gord correctly noted, these Get and Let methods can be accessed
publicly and therefore will allow non-local functions and procedures
that have an instance of your class to modify/view those values.
Duly noted, thanks.
>
PS. You took the "unintersting path" and chose the employee and not
the slot machine ;)
That's the kinda guy I am ;)
>
Cheers,
Jason Lepack

Dec 1 '06 #18

P: n/a
"Albert D. Kallal" <Pl*******************@msn.comwrote in message
news:GkHbh.398409$5R2.297739@pd7urf3no...
>I give some thoughts and helpfull advice here:
Using Class objects with ms-access.
By Albert D. Kallal
Tuesday, September 16, 2003

Why would I want to use a class object in ms-access?

http://www.members.shaw.ca/AlbertKal.../WhyClass.html
Just a quick question Albert.

"when I create and set the myTour = "tour id", then all of the recordsets
for bus company, hotels, rates etc gets loaded into the object."

Could you just expand a little on this please? How does the data get
loaded, do you open a recordset in a subroutine? Still missing the point
here I think!

Thanks.

Keith.
Dec 1 '06 #19

P: n/a

Keith Wilby wrote:
"Albert D. Kallal" <Pl*******************@msn.comwrote in message
news:GkHbh.398409$5R2.297739@pd7urf3no...
I give some thoughts and helpfull advice here:
Using Class objects with ms-access.
By Albert D. Kallal
Tuesday, September 16, 2003

Why would I want to use a class object in ms-access?

http://www.members.shaw.ca/AlbertKal.../WhyClass.html

Just a quick question Albert.

"when I create and set the myTour = "tour id", then all of the recordsets
for bus company, hotels, rates etc gets loaded into the object."

Could you just expand a little on this please? How does the data get
loaded, do you open a recordset in a subroutine? Still missing the point
here I think!

Thanks.

Keith.
Hi, Keith.

Here is a simple example of what I *think* Albert is driving at. Notice
that the "Property Let "Salary_Grade" not only saves the Salary_Grade
you pass it, as in...

objEmployee.Salary_Grade = 3

....but it also looks up the corresponding Annual_Salary and saves that
as well, so you can retrieve it via...

objEmployee.Annual_Salary

....without doing the lookup yourself.
'-----class module code follows-----

Option Compare Database
Option Explicit

Private mstrLastName As String
Private mintSalary_Grade As Integer
Private mcurAnnual_Salary As Currency

Public Property Get LastName()
' return property value
LastName = mstrLastName
End Property

Public Property Let LastName(ByVal vNewValue As Variant)
' store property value
mstrLastName = vNewValue
End Property

Public Property Get Salary_Grade() As Variant
' return property value
Salary_Grade = mintSalary_Grade
End Property

Public Property Let Salary_Grade(ByVal vNewValue As Variant)
' store property value...
mintSalary_Grade = vNewValue
' ... and look up annual salary from reference table
mcurAnnual_Salary = _
DLookup("Annual_Salary", "Salaries", "Salary_Grade=" &
mintSalary_Grade)
End Property

Public Property Get Annual_Salary() As Variant
' return property value
Annual_Salary = mcurAnnual_Salary
End Property

Dec 1 '06 #20

P: n/a
>
"when I create and set the myTour = "tour id", then all of the recordsets
for bus company, hotels, rates etc gets loaded into the object."

Could you just expand a little on this please? How does the data get
loaded, do you open a recordset in a subroutine? Still missing the point
here I think!
Yes, in code I could go

dim tour1 as new clsrides
dim tour2 as new clsRides

tour1.TourID = 123 ' load tour 123
tour2.TourID = 34 'load tour 34

At this point, I have TWO tours loaded in ram, and thus go

msgbox "hotel for tour 123 is " & Tour1.HotelName

msgbox "hotel for tour 34 is" & Tour2.HotelName

So, note how the above was VERY little coding, and I can have TWO tours in
memory at the SAME time. And, even if I just need one in code...it is still
much easer then a bunch of subroutines and global variables that would trip
over them selves....

The class object is over 600 lines of code in total.....

Additional properties of the class object are outlined here:
http://www.members.shaw.ca/AlbertKal...Appendex4.html

The left side does NOT display all properties, but gives you an idea.....
the actual code in the class object for TourID is:

Public Property Let TourId(LoadID As Long)

' Loads/setsup the tour object (pricing etc..)
' DO NOT load if id is same as passed....

Dim myDB As Database
Dim qryBusList As QueryDef

Set myDB = DBEngine(0)(0)

If m_TourId <0 Then
' tour is loaded, check for same tour...
If m_TourId = LoadID Then

' tour already loaded...don't re-load
Exit Property

End If
End If

m_TourId = LoadID

' loads the load rates....

Set m_rstTour = myDB.OpenRecordset("select * from tbltours where id = " &
m_TourId)

With m_rstTour
If .RecordCount 0 Then

m_FromDate = !FromDate
m_Todate = !ToDate
m_HotelId = !Hotel
m_dtDepositDue = m_FromDate - !DaysDue

' load up hotel informaton
Set m_rstHotel = myDB.OpenRecordset("select * from tblHotels where
id = " & m_rstTour!Hotel)

' load up buses attachted to this tour
Set qryBusList = myDB.QueryDefs("qryTourBusListB")
qryBusList.Parameters(0) = m_TourId
Set m_rstBusList = qryBusList.OpenRecordset

' load up seasonal pricing talbes
Call TLoadRates

' set tax tax.
If m_rstTour!NoGst = True Then
m_GstRate = 0
Else
m_GstRate = gblGst(m_FromDate)
m_PstRate = gblPst(m_FromDate)
End If

Else
m_TourId = 0
End If
End With

End Property

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
Dec 2 '06 #21

P: n/a
By the way, that appendix link where I outline that access class object is

is part of larger article here:

http://www.members.shaw.ca/AlbertKal...000000003.html

In that article, I explain why I used class objects in ms-access....

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com

Dec 3 '06 #22

P: n/a
"Albert D. Kallal" <Pl*******************@msn.comwrote in message
news:%Ooch.411871$R63.320926@pd7urf1no...
By the way, that appendix link where I outline that access class object is

is part of larger article here:

http://www.members.shaw.ca/AlbertKal...000000003.html

In that article, I explain why I used class objects in ms-access....

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
Many thanks Albert (and Gord), I shall read though all of this today.

Regards,
Keith.
Dec 4 '06 #23

P: n/a
"Albert D. Kallal" <Pl*******************@msn.comwrote in message
news:GGoch.411863$R63.146206@pd7urf1no...

tour1.TourID = 123 ' load tour 123
tour2.TourID = 34 'load tour 34

At this point, I have TWO tours loaded in ram, and thus go

msgbox "hotel for tour 123 is " & Tour1.HotelName

msgbox "hotel for tour 34 is" & Tour2.HotelName
<snip>
>
the actual code in the class object for TourID is:

Public Property Let TourId(LoadID As Long)
Albert,

Dumb question: how do you supply the argument to the property? You have
"tour1.TourID = 123" but wouldn't you get an "argument not optional" error
there?

Thanks for your patience, I'll get the hang of this yet!

Keith.
Dec 4 '06 #24

P: n/a

Keith Wilby wrote:
"Albert D. Kallal" <Pl*******************@msn.comwrote in message
news:GGoch.411863$R63.146206@pd7urf1no...
>
tour1.TourID = 123 ' load tour 123
tour2.TourID = 34 'load tour 34

At this point, I have TWO tours loaded in ram, and thus go

msgbox "hotel for tour 123 is " & Tour1.HotelName

msgbox "hotel for tour 34 is" & Tour2.HotelName
<snip>

the actual code in the class object for TourID is:

Public Property Let TourId(LoadID As Long)

Albert,

Dumb question: how do you supply the argument to the property? You have
"tour1.TourID = 123" but wouldn't you get an "argument not optional" error
there?
Property Let statements behave differently than Function and Sub
statements do. 123 *is* the argument.

Dec 4 '06 #25

P: n/a
"Gord" <gd*@kingston.netwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
>>
Dumb question: how do you supply the argument to the property? You have
"tour1.TourID = 123" but wouldn't you get an "argument not optional"
error
there?

Property Let statements behave differently than Function and Sub
statements do. 123 *is* the argument.
Thanks for your patience Gord. Another dumb question but hopefully the
answer will make the penny drop. In a simple example, if I have a table
"tblHolidays" with fields "ID" and "Country" and I have a class
"clsHoliday", how do I set and retrieve a "Country" property? I'm calling
the class using:

Dim hlHoliday As clsHoliday
Set hlHoliday = New clsHoliday
hlHoliday.HolID = "ABC123"

Here's the class code:

Property Let HolID(strID As String)

Dim db As DAO.Database, rs As DAO.Recordset
Dim strCountry As String
Set db = CurrentDb
Set rs = db.OpenRecordset("Select * from tblHolidays Where ID = '" & strID &
"'")
With rs
If .RecordCount 0 Then
'*** What goes here to get the other properties? ***
End If
End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Property
Dec 5 '06 #26

P: n/a
Keith Wilby wrote:
"Gord" <gd*@kingston.netwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
Dumb question: how do you supply the argument to the property? You have
"tour1.TourID = 123" but wouldn't you get an "argument not optional"
error
there?
Property Let statements behave differently than Function and Sub
statements do. 123 *is* the argument.

Thanks for your patience Gord. Another dumb question but hopefully the
answer will make the penny drop. In a simple example, if I have a table
"tblHolidays" with fields "ID" and "Country" and I have a class
"clsHoliday", how do I set and retrieve a "Country" property? I'm calling
the class using:

Dim hlHoliday As clsHoliday
Set hlHoliday = New clsHoliday
hlHoliday.HolID = "ABC123"

Here's the class code:

Property Let HolID(strID As String)

Dim db As DAO.Database, rs As DAO.Recordset
Dim strCountry As String
Set db = CurrentDb
Set rs = db.OpenRecordset("Select * from tblHolidays Where ID = '" & strID &
"'")
With rs
If .RecordCount 0 Then
'*** What goes here to get the other properties? ***
End If
End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Property
Hi, Keith. Try this:

'-----start of class module----
Option Compare Database
Option Explicit

Private mstrHolID As String
Private mstrCountry As String

Public Property Get HolID() As String
' return the property value
HolID = mstrHolID
End Property

Public Property Let HolID(ByVal strID As String)
Dim db As DAO.Database, rs As DAO.Recordset

' save the ID property value
mstrHolID = strID

' retrieve other attributes
Set db = CurrentDb
Set rs = db.OpenRecordset( _
"Select * from tblHolidays Where ID = '" & strID & "'")
With rs
If .RecordCount 0 Then
'*** What goes here to get the other properties? ***
mstrCountry = rs!Country
End If
End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Property

Public Property Get Country() As String
' return the property value
Country = mstrCountry
End Property
'-----end of class module----
'-----start of usage example----
Sub HolidayTest()
Dim hlHoliday As clsHoliday

Set hlHoliday = New clsHoliday
hlHoliday.HolID = "ABC123"
Debug.Print hlHoliday.Country

Set hlHoliday = Nothing
End Sub
'-----end of usage example----

Dec 5 '06 #27

P: n/a
"Gord" <gd*@kingston.netwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
>
Hi, Keith. Try this:
<snip>

Duh, I feel so stupid now ;-) The penny has finally dropped, for this
example at least. Many thanks again Gord.

Keith.
Dec 5 '06 #28

This discussion thread is closed

Replies have been disabled for this discussion.