473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Custom Classes

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
27 2362
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
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

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
"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
"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
"Lyle Fairfield" <ly***********@aim.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
<snip>

Thanks Lyle.
Nov 30 '06 #7
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
"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
"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

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
"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
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
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
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
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
"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
"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
"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

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
>
"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
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
"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
"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

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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Neil Zanella | last post by:
Hello, In Python, classes are objects. But there is no way to custom print a class object. This would require some syntax such as the one commented out below: With the current "foo =...
7
by: Adam | last post by:
Im trying to add an httphandler for all *.sgf file extensions. I have developed the handler, 1. installed it into the gac 2. added it to the machine.config: <httpHandlers> <add verb="*"...
5
by: mtv | last post by:
Hi all, I have the following code: ================================ Webservice side: public class MyWS: WebService { private myLib.DataObject curDataObject;
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
8
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.