We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Using VBA Class Modules

Saleve
Saleve asked
on
Medium Priority
3,022 Views
Last Modified: 2013-11-29
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
Comment
Watch Question

Jim Dettman (EE MVE)President / Owner
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
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.
Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007
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




DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
oops ...

" Wherein they are no full inheritable,"

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

mx
Jim Dettman (EE MVE)President / Owner
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017

Commented:
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.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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
Jim Dettman (EE MVE)President / Owner
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.