• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3008
  • Last Modified:

Using VBA Class Modules

Hello,
I am a beginner/intermediate user of VBA but I would like to get much better.  I am particularly interested in learning how to use class modules.  I have read the chapter on this subject in the Litwin, Getz, Gunderloy book and I am left scratching my head.  I have also read Beginning Access VBA 2003 by Cosnell.  I learned a lot, but felt that her "How it Works" sections just repeated the preceding section rather than give an explanation.

I am looking for a book/resource that will explain this concept step by step with many more detailed examples, especially concerning more advanced techniques such as building class hierarchies.  (I can already create very basic class modules.)  Suggestions would be most appreciated.

Thank you and regards,
Saleve
0
Saleve
Asked:
Saleve
  • 3
  • 3
3 Solutions
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Saleve,
<<I am looking for a book/resource that will explain this concept step by step with many more detailed examples, especially concerning more advanced techniques such as building class hierarchies. (I can already create very basic class modules.) Suggestions would be most appreciated.>>
Unfortunately, your not going to get far with VB or VBA as neither has fully inheritable classes. Basic classes is it. VFP (Visual Fox Pro), C++, and .Net languages (including VB.NET) are all fully OOP based and you would be able to build hierarchies, but not with VB or VBA.
JimD.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
For my money, the concept of class modules is a viable one regardless of the limitations of VB/VBA. I find them invaluable when dealing with unbound forms and such and while the environment doesn't strictly support full use of OOP it is, to me, a very valuable skill to learn.

I found this book to be a valuable tool when learning to build and work with class modules:

Visual Basic 6 Business Objects by Rockford Lhotka

It is geared towards VB 6.0, but virtually every concept can be applied to Access/VBA.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"Unfortunately, your not going to get far with VB or VBA as neither has fully inheritable classes."

JD ... I have to respectfully disagree with this.  Wherein they are no full inheritable, that certainly does not render Class modules in Access useless.  I have a dozen Class modules in my Library MDA, and 2-3 of those in daily use in most of my apps.  The others are more special purpose.  And of course, Lebans (http://www.lebans.com/) et.al., have certainly made good use of Class modules - in many of the apps we here at EE are always referring people to.

"I am particularly interested in learning how to use class modules.  "
Saleve:

Turns out ... if you have ever put any code in a Form or Report, then in fact you have worked with Class modules and didn't know it ... because, technically, Form and Report modules are in fact Class modules.

The main difference between a Form/Report class module and an external Class module is that external Class modules have a Class_Initialize and Class_Terminate event, whereas forms do not.  Forms however have Open, Load, UnLoad and Close events (reports have Open/Close) which are 'sort of' similar to the Initialize and Terminate event of external Class modules.

I will try to upload a 'Timer' class module (demo) example over the weekend, which shows in a simple manner why class modules can be (and are) very useful.

mx




0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
oops ...

" Wherein they are no full inheritable,"

make that " Wherein they are not fully inheritable ..."

mx
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Mx,
<<JD ... I have to respectfully disagree with this.>>
  I didn't say it rendered classes useless in Access, but in regards to programming with classes in a OOP based fashion, it does.  Within VBA you can have a pointer to another class and create an object hierarchy.  You can also use the Implements keyword to get you part way there in forming a a class hierarchy, but VBA does not provide implementation inheritance (it only provides interface inheritance).  Thus you are cut off from the full power of classes as you wound find in other languages such as C++ or Visual Foxpro.
  He said he was looking for examples on advanced class concepts "especially concerning more advanced techniques such as building class hierarchies. "   Without true inheritance, your are not going to get far with that.
  He also mentioned " (I can already create very basic class modules.)  ".  With VBA that is about as far as you can get really and is what most of us stick with in VBA; simple object classes.  Look at any Access app that uses classes and I would hazard a guess that 99% of them don't do anything more then simple object classes that sink events.  
  If I contrast that to something I would write in VFP, where everything is a class, that's quite a difference and fundamentally a different way of programming.
FWIW,
JimD.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Well, my class modules do a lot more than 'sink events'.  One power of classes in VBA is that you can instantiate multiple instances of the same Class, a very powerful concept.  The fact that we don't have full inheritance does not bother me in the slightest really.  And of course, this is the reason you can open multiple instances of the exact same Form (from code).

I have this little 12 month calendar.  It's one main form ... to hold 12 instances of the same exact subform - which is the calendar. VIA code, when you open the main form, of course you see 12 different months.  A little off my point above, but it's still pretty dang cool.

mx
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Mx,
<<One power of classes in VBA is that you can instantiate multiple instances of the same Class, a very powerful concept. >>
  That's a given in any type of language that has the capability of having classes and is nothing special in terms of the implementation of classes.
JimD.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now