469,304 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

VBA: Class Modules Questions

Alright...
Does anyone know if you can create a class that has a constructor?

I would like to create a BUTTONMANAGER class that handles my buttons
for me...
I am doing this in Access for those who care.

I can't seem to get class_initialize to have required arguments in the
initializer. Of course I just assumed that the class_initialize was the
constructor... Maybe not?

In case I am not being clear? I want to be able to do something like
INSIDE my VBA...

Public Sub button_onclick()
Dim test as ButtonManager
Set test = New ButtonManager( "My Name Is Bob", 200 )
End Sub

Can you do this with VBA?
My main experience with OOP is in PHP, Javascript and a little in
Java...

Help me Mr. Popeil...
Thanks in advance!

Nov 13 '05 #1
5 17785
randomblink wrote:
Alright...
Does anyone know if you can create a class that has a constructor?

I would like to create a BUTTONMANAGER class that handles my buttons
for me...
I am doing this in Access for those who care.

I can't seem to get class_initialize to have required arguments in the
initializer. Of course I just assumed that the class_initialize was the
constructor... Maybe not?

In case I am not being clear? I want to be able to do something like
INSIDE my VBA...

Public Sub button_onclick()
Dim test as ButtonManager
Set test = New ButtonManager( "My Name Is Bob", 200 )
End Sub

Can you do this with VBA?
My main experience with OOP is in PHP, Javascript and a little in
Java...


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Unfortunately, the Class_Initialize doesn't take parameters like Java
constructors. Usually, you have to set properties after you've
instantiated the object.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQjYcKIechKqOuFEgEQJmXgCgnLZ0fOXp2bUaHMhiDkYHsH eu24oAn17Z
tI3PsIgbSc7qPADYZcXmpnhV
=yDZP
-----END PGP SIGNATURE-----
Nov 13 '05 #2
You don't mention if you are wanting to create a "set" of buttons that you
use over and over for each form.

If you need the above, you can do two things:

Simply create a form with the buttons on it and also the code for each
button. You then drop this form into any existing form as a sub-form.
At load time, a copy of the sub-form as a class object is created for you.
And, this takes NO code. So, for example, the code behind a button to force
a "save" is normally

me.Refresh

However, since our code is now in a sub-form, then you could write behind
the button

me.Parent.Refresh

Thus, this approach gives you a instant drop in set of "buttons" that can
function on as many open forms as you have.

If you are NOT talking about a reusable set of buttons, then I don't see a
whole lot of need to send the events out to a class object that handles the
button code (what do you gain?...just have the bunion call the "general
code", or have the button execute methods of the class object). However, if
you simply want to tie any button on a form to a instance of your button
object, then what you can do is simply instance a new copy of the button
object at form load time. Now, for each button in the form, simply place the
following in the buttons on-click property:

=MyClickCode()

or, for particular stuff, you can go:

=MyClickCode("someParm txt",123)

Note that this means we do NOT have to write a event code snip for reach
button, but just place the above direct in the controls property sheet.
Further, you can highlight as many buttons as you want in design mode, and
then type in the function name once, as above =MyClickCode() , and you just
set ALL of those buttons to run the code (this would not make sense for
buttons with specify parmars...but I just wanted to give you some tips
here).

Of course, we have to place ONE function called =MyClickCode() (and with
parameters if that is what we want)

Function MyClickCode

dim ctrl as control

set ctrl = me.ActiveControl

msgbox "name of control clicked = " & ctrl.name

'....do whatever...or even pass the control object to your class object

set clsButtionOjbect.activeContorlclick = ctrl

Even better would be to pass the screen object, and the contorl ref to
the object..and it it work.

clsButtionObejct.MyClick(me, me.activeControl)
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 13 '05 #3
No, the simplest way around this is to create a procedure which takes your
initilisation arguments

Public Sub button_onclick()
Dim test as ButtonManager
Set test = New ButtonManager
Call test.Init( "My Name Is Bob", 200 )
End Sub

Havinf said that I would always reccomend doing OOP properly and implement
interfaces rather than using the
co-class.
--
Terry Kreft
MVP Microsoft Access
"randomblink" <ra*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Alright...
Does anyone know if you can create a class that has a constructor?

I would like to create a BUTTONMANAGER class that handles my buttons
for me...
I am doing this in Access for those who care.

I can't seem to get class_initialize to have required arguments in the
initializer. Of course I just assumed that the class_initialize was the
constructor... Maybe not?

In case I am not being clear? I want to be able to do something like
INSIDE my VBA...

Public Sub button_onclick()
Dim test as ButtonManager
Set test = New ButtonManager( "My Name Is Bob", 200 )
End Sub

Can you do this with VBA?
My main experience with OOP is in PHP, Javascript and a little in
Java...

Help me Mr. Popeil...
Thanks in advance!

Nov 13 '05 #4
Implements? Interfaces?

Man, I wish I had that level of sophistication... chuckle.

Sadly, all the OOP I know I have learned on my own. And nothing I have
needed to know has mentioned interfaces or implements. Oh, I have SEEN
them when perusing manuals and books, but I have so little time to do
what I want that I tend to spend the time I have learning what I NEED to
know.

SO if you can explain, in the textbox given you for a reply on this
board, why I would want to use an Interface or Implement and then where
I can learn this online?

I would REALLY appreciate it...
Thanks for replies all...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5
Look for books by Ted Pattison (with the word COM in the title) and just
read the first few chaptes to get an excellent insight into what interfaces
are and what they do for you.

A short (and wholly inadequate explanation of the implementation of
interfaces follows).

An interface is the external face of what your class can do.

In VB(A) classes are given a default interface which matches the signature
of your class, so say you have a class called MyClass with an Init method,
VB(A) automatically creates an interface (called _MyClass) for that class.

What you can do is create your own interfaces for the class and then use
them. Lets say you had a class which dealt with sales orders (cOrders) you
might implement an interface on this which gave you the customer details for
the order, or the dates relevant to the order, for example create a class
module call it ICustomer and put the following in it:-

Property Let Code(RHS As String): End Property
Property Get Code() As String: End Property
Property Get Name() As String: End Property

Then create a class module call it IOrderDates and put the following in it:-

Property Let OrderDate(RHS As Date): End Property
Property Get OrderDate() As Date: End Property
If you then put the following lines in the cOrders class in the declarations
section

Implements ICustomer
Implements IOrderDates

Then click in the combo at the top which says (General) you'll see 2 new
entries in there ICustomer and IOrderDates.

If you select ICustomer the the lefthand combo box will let you select each
of the properties we declared in the ICustomer interface and similarly with
IOrderdates.

When you've selected all the properties yout cOrders class will look a bit
like this (I've added some commented lines to make it easier to seee where
the interfaces are.

Option Compare Database
Option Explicit

Implements ICustomer
Implements IOrderDates
'************************************************* *
' ICustomer implementation
Private Property Get ICustomer_Code() As String

End Property

Private Property Let ICustomer_Code(RHS As String)

End Property
' ICustomer implementation
'************************************************* *

'************************************************* *
' IOrderDates implementation
Private Property Get IOrderDates_OrderDate() As Date

End Property

Private Property Let IOrderDates_OrderDate(RHS As Date)

End Property
' IOrderDates implementation
'************************************************* *

You can now do things like

Dim cO as cOrders
Dim iC as ICustomers

Set cO = New cOrders
Set iC = cO

' At this point you can start using teh ICustomers interface
With iC
.Code = "xxxyyy"
Msgbox .Name
End with

Set iC = nothing
Set cO = nothing
In your cOrders class module you can then write code in the interfaces as
you normally would in order to achieve the results you want.
--
Terry Kreft
MVP Microsoft Access
"Brian O'keefe" <ra*********@yahoo.com> wrote in message
news:42**********@127.0.0.1...
Implements? Interfaces?

Man, I wish I had that level of sophistication... chuckle.

Sadly, all the OOP I know I have learned on my own. And nothing I have
needed to know has mentioned interfaces or implements. Oh, I have SEEN
them when perusing manuals and books, but I have so little time to do
what I want that I tend to spend the time I have learning what I NEED to
know.

SO if you can explain, in the textbox given you for a reply on this
board, why I would want to use an Interface or Implement and then where
I can learn this online?

I would REALLY appreciate it...
Thanks for replies all...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Steve Jorgensen | last post: by
18 posts views Thread by Ann Scharpf via AccessMonster.com | last post: by
13 posts views Thread by André | last post: by
1 post views Thread by Marcel Nihon | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.