Access OOP examples wanted (2 Viewers)

jdraw

Super Moderator
Staff member
Local time
Today, 14:43
Joined
Jan 23, 2006
Messages
15,364
Here is a link that has some "oop" features to get current subform container name. Thanks to Markk and stopher; and IvanM for posing the question.
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 14:43
Joined
Jan 23, 2006
Messages
15,364
Here is a more recent youtube video dealing with class modules. It is by Steve Bishop (Programming) who has a number of vba related videos. He discusses an approach and the logic to validate data entered into textboxes on a form and creates class modules step by step to do that validation. It is the context and thought processes for needing/creating/coding and testing the class modules that I found helpful.

It references the RubberDuckvba vbe addin but it is the class module coding/usage that is demonstrated clearly for the viewer.


Also: Intro guide to class modules can be found here
 
Last edited:

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 14:43
Joined
Oct 17, 2012
Messages
3,276
While I am thinking about it, I'm attaching a ZIP file containing a polymorphism demo I threw together for a coworker. (It's a ZIP file because uploads of the basic accdb file are failing for some reason.)

Classes are intimately bound up in polymorphism, so this seemed the appropriate place to put the file.
 

Attachments

  • Polymorphism.zip
    767 KB · Views: 310

NauticalGent

Ignore List Poster Boy
Local time
Today, 14:43
Joined
Apr 27, 2015
Messages
6,286
On a whim, I consulted my Access VBA Inside and Out book (Andrew Couch) regarding Class Modules. Some very good examples as well as detailed instructions on what they do and how to design them.

Where his book falls short however is how to determine when to use them. Fortunately, the article that JDraw shared bridges that gap.

I would share some of that code here but it be a direct violation to the terms on purchasing the book.

I will try to wrap my feeble brain around this...
 

jdraw

Super Moderator
Staff member
Local time
Today, 14:43
Joined
Jan 23, 2006
Messages
15,364
I would like to see a video(s) (youtube/screencast???) to take a problem/opportunity from start to finish describing the business issue; recognizing a 'business object'; deciding/rationalizing the use of a class approach; developing the class with clear dialog and code;... This could be a series possibly. (very beginner ...additional complexity)

NG,
I think this could be a random topic/issue and use an approach similar to a book example without "infringing/violating" purchasing terms. I note that Frothingslosh said he uses classes regularly, so he may have more insight on how to bring classes to more vba developers/users.

Most of what I've seen or read indicates it's a mindset change, but once you "get it" you'll use classes more often.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 14:43
Joined
Apr 27, 2015
Messages
6,286
Most of what I've seen or read indicates it's a mindset change, but once you "get it" you'll use classes more often.

AC said almost the exact same thing at the end of the chapter:
"Some might argue that rather than using classes, which involves constructing a framework, you could more simply have built a re-useable library. This line of argument nearly always holds; thus, the decision to use classes becomes a question of whether it seems more intuitive and natural than using a traditional code module."

And at the very beginning before he proceeds:
"Many Microsoft Access developers take a look at classes and then give up because they have difficulty seeing the benefit and justification for using classes. It’s true that much of what can be achieved with a simple class can also be achieved by using libraries of code, and that to build classes you often need to put in more effort during the initial development, but there are benefits in using classes that will be explored in this chapter..."

I suppose if I made a living developing applications for clients, this would be a handy tool to have. As it is, I only maintain one as a collateral to my primary purpose which is logistics planning.

All that aside, the ability to create a custom class of objects that can be used throughout your application - more specifically, having Intellisense provide you your objects as you code - is just plain NEAT.

My biggest hurdle at this point is to see how I can fit this in so that I can convince myself that the "juice" is worth the "squeeze".
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 14:43
Joined
Oct 17, 2012
Messages
3,276
Well, keep in mind that most of the stuff we work with in VBA are actually classes - every form object, every table object, DoCmd; they're all classes with properties and methods that we use.

Basically, when I'm writing code for my projects, I ask 'Is this a 'thing' or part of a related group of functions, or is this just a simple function?'. A function that simply, say, accesses the GetUserName function from the Windows API will remain a standalone function, whereas a series of functions that loads a recordset, validates the data, exports the data to Excel, and then formats the data will probably wind up bundled together in a single class. Same thing with user data - if your interaction with user data is a simple "are they allowed in yes/no" deal, then a stand-alone function is fine, but if you need to confirm access, pull their real name, save their permissions, save their email address, and deal with special access 'flags', and you need to keep referring to it throughout the use of the app, then a class is the way to go.

You may have seen that god-awful 'Importing data from Excel' procedure I posted in the repository a few years ago. A much more modern (and much faster) version of it I wrote at my previous employer (where I once again ran into the SAME FREAKING PROBLEM THAT SPAWNED IT IN THE FIRST PLACE :mad: ) uses classes and collections instead of arrays, largely because despite being a hair slower, they're MUCH easier for your successors to understand.

Seriously, though, the trick to the right mindset is just learning to look at something and realize that it's either a 'thing' you'll be dealing with a lot or doing a lot of complicated work with, or a collection of related stuff that really should be just bundled together (which is, honestly, what DoCmd is).
 
Last edited:

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 14:43
Joined
Oct 17, 2012
Messages
3,276
And honestly, classes stopped being intimidating once I just bit the bullet and wrote a couple. Beginners should just get a quick reference (I think Chip Pearson had a good one) showing how to set up properties and the Initialize and Terminate procedures, and then just write a couple and work with them.

I have simple examples in my Polymorphism example above, and my progress meters post over in the Repository is completely class-based.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:43
Joined
Sep 21, 2011
Messages
14,046
AND...I picked up a neat little trick on how to comment out a whole block of code in instead of "tip-toeing through the tulips"!

I'f love to know about that, as I frequently comment out code when trying new things.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:43
Joined
Sep 21, 2011
Messages
14,046
Wow, that is so handy, and it has been there all the time? :(

Thanks for that.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 14:43
Joined
Oct 17, 2012
Messages
3,276
Wow, that is so handy, and it has been there all the time? :(

Yep. I always assumed everyone already knew about it.

Just as an FYI, if you use SQL Server Management Studio, it has the same buttons available there, too.
 

jdraw

Super Moderator
Staff member
Local time
Today, 14:43
Joined
Jan 23, 2006
Messages
15,364
Tony,

It's funny you mention the wiseowl tutorial. I've seen many of these on classes and web scraping and find them excellent --although geared to Excel, but all done using vba. And easily portable to Access. There are several videos, all available free from wiseowl site. (see post #20 in this thread)

There are also some great youtube videos by Paul Kelly (Excel Macro Mastery) --collections etc.
He offers free webinars a couple of times a week (latest is http://excelmacromastery.us10.list-...ddd2e9d6edafa28440&id=02c97b38b4&e=d2b706b539 ). He is selling ( at a discount to webinar viewers) an Excel application product (http://excelmacromastery.us10.list-...ddd2e9d6edafa28440&id=0e427be75f&e=d2b706b539) for those interested. It is a handbook with 10 full blown vba applications. I'm not an excel person, but find his presentations quite well done and informative.

jack
 
Last edited:

Users who are viewing this thread

Top Bottom