472,328 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and...
18
by: Ann Scharpf via AccessMonster.com | last post by:
I am not sure which would be the best place to post this question, so I'm posing it here with Access general questions. I have reached the point...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the...
0
by: peridian | last post by:
Hi, I have the below code to transfer code modules between Access database files. I have three problems, only one of which is annoying. a) How...
3
by: olle | last post by:
How to deal with a VBA-project that is damaged? Hi everyone. I am BigOlle from sweden and I have been working with Accees for ten years I am...
1
by: Marcel Nihon | last post by:
Hello, I develop an application in Access 2003. The whole of the modules and modules of class contains approximately 9700 lines of code VBA / ADO....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.