Reputation: 1341
I know that classes are templates for creating objects. But we cannot create objects in VBA. I am new to VBA. I don't understand the purpose of class modules in VBA
Upvotes: 1
Views: 5494
Reputation: 383
Of course you can create objects in vba:
If you named your classModule: "YooClass" then you can create a object by:
Dim myObject as YooClass
Set myObject = new YooClass
I also create a non-class-module called ObjectFactory with this code:
Public Function Yoo(...) As YooClass
Set Yoo= New YooClass
Call Yoo.Init(...)
End Function
this works as a constructor. So I can write:
Dim myObject as YooClass
Set myObject = new Yoo(...)
or call a function directly like this
Call Yoo.MyFunction(...)
This is some kind of a "static" function-call (or as close as you can get in vba).
If you want more information about classes in VBA (or in general): There are a lot of tutorials for this with a lot of explanations and examples. Like this: https://analystcave.com/vba-vba-class-tutorial/
Upvotes: 1
Reputation: 49264
Access most certainly allows you to create objects. Because Access does not support inheritance, and things like "over loading", then Access is not considered a full Object orientated programming system.
However, it has "some" parts that are considered OO (Object orientated) and you can well use such a approach. I explain in the following article as to when and why you might want to use + create objects in Access:
http://www.kallal.ca/Articles/WhyClass.html
So Access is not full OO, but it certainly has the abllity to create class objects like you would say in vb.net, or c#. I use class objects quite often now, and the above article will give you some hits as to when, and why you would want to do this in Access.
Upvotes: 2