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

How to create a constructor(OnLoad-Event) for a user-defined class?

P: 13
Well, I ask this question because I'm rather familiar with C++ than VB.

I've just tried to create a class in VBA and found that I have to write some certain lines every time I want to create a new instance of the class.

So, isn't there a way to create a autorun subroutine for initializing a user-defined class?, like "constructor" in C++.

Thanks
Aug 22 '08 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 112
Yes, for every class if you include this sub it will run on instantiation of the object:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Class_Initialize()
  2.    'Do whatever here
  3. End Sub
  4.  
Unfortunately vba doesn't offer method overloading so you can't easily pass arguments to the constructor, but it has a limited ability to do what you are after.
Aug 22 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
While you can create user-defined classes in Access VBA, it's not an important feature of Access, like it is in C/C++. Few developers use them, and thus VBA gives little support to them. The fact of the matter is, Access addresses most of tasks C/C++ developers use them for with native Access/VBA functions.

Welcome to Bytes!

Linq ;0)>
Aug 22 '08 #3

P: 13
Thanks for the good advices.

I am self-taught and would never find this out by searching in the built-in help.
Are there any better references for MS Access, which I try to find what I want before posting in this community?
Aug 23 '08 #4

missinglinq
Expert 2.5K+
P: 3,532
First line of defense would be search here or on other Access sites. Then use a search engine. I usually lead with +"ms access" then an appropriate, short description, like "Cascading comboboxes."

In a more general vein, spending some time here and elsewhere "browsing" thru posts is a great way to pick up pointers! You can be assured that anything you come up against has been encountered by others, and the answers may already be here.

Here's a list of tutorials, listed from the easiest to understand to the more difficult.

Chrystal has an excellent “basics” tutorial:
http://www.accessmvp.com/strive4peace

This is a little more advanced, but still a great starting point, having 23 well written, well defined, clearly named chapters.
http://www.functionx.com/vbaccess/index.htm

Jeff Conrad's resources page:
http://www.accessmvp.com/JConrad/acc...resources.html

The Access Web resources page:
http://www.mvps.org/access/resources/index.html

Allen Browne's extensive listing of tutorials:
http://allenbrowne.com/links.html#Tutorials

Linq ;0)>
Aug 23 '08 #5

P: 13
Got it, thanks a lot.
Aug 25 '08 #6

FishVal
Expert 2.5K+
P: 2,653
Fathers,

I would agree with the following:

VBA has almost no pointers arithmetics.
VBA does not support class inheritance, polymorphysm and many other features of OOP.
VBA does not support function/operator overload.
VBA is macrolaunguage and could not be recognized as true programming launguage.

But I would not agree with a statement:

VBA OOP features are too poor to even try and many Access developers do not use them because they are too good in OOP. ;)

Every Access developer no matter whether he/she is aware of it is doing OOP programming. ;)

Each time you create a new form or report, you create a new class - Access automatically names it as Form_<formname> or Report_<reportname>.
Form/Report module is actually (to some extent) a class module.

Every public function/sub declared in a form/report module is method of the class. It is not used by many Access developers but properties and events could be declared in form/report module just like as in "regular" VBA class module.

The class may be instantiated in regular way, after which all its properties/methods/events will be available via correspondent object variable.

And, actually, I would stay that VBA OOP could greatly enhance database design and make things easier and more reliable. To say nothing about numerous little tricks very hard or even impossible to implement without dealing with VBA explicitely. ;)

Example: Starting MS Access from vba

Kind regards,
Fish
Aug 26 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.