473,398 Members | 2,403 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,398 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 18087
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 class module? For example, say I have a data...
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 more committed to removing duplication in code at a...
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 obvious in a fully OOP language, but is not at...
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 many times in Word and in Access where my ignorance...
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 class and module and if i understand it right, a...
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 basics of variable scope in VBA for MS Access. For...
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 do you prevent the Save As box from appearing...
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 now working on a project that started in Access97...
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. In the editor Visual BASIC, the "Debug -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.