Why use classes in VBA?

Sandra Smith
Sandra Smith used Ask the Experts™
on
This is mroe of a why question as opposed to a how question.  I have been looking at classes and trying to understand the benefits.  I really don't see any so if anyone has examples or explanations of why they are considered good programming usage, would appreciate.

Sandra
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
Imagine you had an application wherein you need to start/pause/stop multiple timers.  I have a Class Module that will handle this.  One Class Module will handle N instances of the timer.  

If you had to do this w/o a class module, it would be non-trival ... probably the same timer function repeated multiple times.  Not so with a Class Module ... you can instantiate as many instances as you need.

And you may not know this, but a Form/Report module is ... a Class Module :-)

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

Commented:
Further, should it be necessary, you can expose ONLY the necessary Properties and/or Methods in the class to the 'outside' world (aka, other developers, etc).  And again, using the timer class example ... what if you needed to modify the code.  Class module - on place, self contained.  But if you had multiple timer Functions, then multiple changes ....

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

Commented:
Another example ... I have a Class that handle Multi-Select list boxes, reading/writing selections to temp tables, etc.  This way I can (and do) have several MSLB's on one form, all operating on a separate instance of the Class.  Otherwise, a LOT more code, or more complex code would be required.

mx
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
Joe,

Would like to see that MSLB class, if you feel like sharing.   ;-)

Dale
Sandra SmithRetired

Author

Commented:
MX, I think I am just dense.  So, if I have employees that I need to loop through, names, ID's, job title, hire date and salary, how would making an employee class be better than using a recordset and looping through each record in the set to, say, increase salary by 10% for each employee in California?  Or would this even be a candidate for a class?

Sandra
Database Architect / Application Developer
Top Expert 2007
Commented:
Not sure that fits the Class paradigm per se

But I do like the 10% increase in CA !!
Sandra SmithRetired

Author

Commented:
LOL!  Wishful thinking on my part!  

So, perhaps part of my issue is not understanding when to use them.  But you have given some good information, I need to keep investigating and experimenting.  Thank you.

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

Commented:
You might want to check this out:

Introduction To Classes in VBA
http://www.cpearson.com/excel/Classes.aspx

So, you are where in CA ?

OH ... Bay Area .. 94xxx ...

mx
Sandra SmithRetired

Author

Commented:
Yep, SF is the stomping grounds, downtown to be exact.  All those banks, trading companies, mortage brokers.......  

Thanks for the link and will definitely check it out.

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

Commented:
Yeah ... prime area for hackers ... $$$$$$ $$$$$$ $$$$$

My plan WAS ... to git up to the Bay Area before the end of 2011.  OOPS ... too damn busy down here. Have 3-4 friends up that way I have not seen in many years ... Palo Alto area. Plus, the best blues guitar player on the planet ... Chris Cain, lives circa San Jose, and does not get down here often enough.

mx
Sandra SmithRetired

Author

Commented:
Blues?? Sorry, I am old rocker from way back.  Cher, Beatles, etc anything from the 60/70's and I am in music heaven.  Ok, so now I am singing "Halfbreed" for the rest of the day!  LOL!

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

Commented:
Well ... yeah, blues, classic rock and 60 Motown ... that's what I do ...
>>>  Crank It Up
Top Expert 2011

Commented:
mx,  I did not know you were so artistic. Very cool Dude!
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
thx bro ... are you going to the Summit ?

mx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial