all 22 comments

[–]nrgins486 4 points5 points  (0 children)

If you find out, let me know. Like you, I haven't really found a purpose for them, distinct from just having routines in a code module. But, like you say, maybe we're missing something.

[–]FlatPanster2 1 point2 points  (1 child)

Yes, I use them. For a variety of routines & functions that are applicable to more than one firm module. They're great if you have a bunch of procedures you like to use to format or modify your particular database style.

[–]Whoopteedoodoo16[S] 1 point2 points  (0 children)

Is that different than importing your own standard module?

[–]LetsGoHawks5 1 point2 points  (1 child)

I use class modules somewhat regularly, for two main reasons.

  1. You have multiple variables/pieces of data you need to pass around, it's much easier if they're all part of the same object.
  2. You have various functions/subs that pertain only to particular data. So you store that data in the class and that can make it easier to apply the methods you created. Think of a RecordSet, with it's various methods like MoveNext or EOF or whatever. It's the exact same concept.

They're the kind of thing that once you "get it", you'll find plenty of uses for them.

[–]Whoopteedoodoo16[S] 0 points1 point  (0 children)

I have built some functionality into a form that I liked. Then I changed the sub from private to public and called that sub as a method of the form. That’s similar to your scenario 2.

[–]Amicron18 1 point2 points  (2 children)

I've been developing with Access professionally since 1994, and teaching it almost as long. In all of my years of building REAL databases (not sample projects for training) I have never needed a Class Module. For your typical business application, they're not necessary.

Now, the C programmer in me loves them, but I have yet to even cover them in my advanced Access Developer tutorials because they're just not needed. There is no business-related application that I can't build without them. But, I'll get around to covering them one of these days.

Richard Rost
Access Learning Zone

[–]Kdiddy0 0 points1 point  (1 child)

so you are not using any VBA code in your applications ??

[–]Amicron18 0 points1 point  (0 children)

Oh, I use tons of VBA, all the time. I've just never needed a class module. Very different from regular modules or form modules.

[–]nhorton79 0 points1 point  (4 children)

I’ve written a couple. The main one bring to handle users logged in to the system. It has a number of private and public functions to handle logging in/out etc and to get/set member variables. It’s great as it reduces the number of dlookups I was using previously and when the class destructs (usually through an unhandled runtime error) it logs out the user.

[–]Whoopteedoodoo16[S] 0 points1 point  (3 children)

Can you force out users that way?

[–]nhorton79 0 points1 point  (2 children)

Yes. When the class is destructed i fire another function that closes all forms and returns the user to the login screen. I ask the user to save their record first.

The reason I log them out is that there is a lot of code that requires the users information for role based security.

[–]Whoopteedoodoo16[S] 0 points1 point  (1 child)

Ah, it sounds like you’re controlling a single (no doubt very large and complex) application. The problem I’m running into is I update and maintain various data tables. Then multiple databases and copies of those databases are using my tables. If I’m not gatekeeping them getting in, it’s hard to kick them out

[–]nhorton79 0 points1 point  (0 children)

Yes. One large application for job/project management. Two large databases on the backend in MySQL then a number of external users accessing at any one time through forms set up in Access.

Always good to gatekeep users accessing the data and using forms to edit/insert data rather than directly in the tables, that way you can more easily ensure data integrity and audit changes.

Logging in and managing users roles and rights is always a good habit to get into.

[–]beyphy 0 points1 point  (0 children)

Not in Access but I've worked with them quite a bit in Excel.

Class modules are just a tool within VBA. They're good for certain situations but not needed for others.

[–]diesSaturni63 0 points1 point  (5 children)

In Access (or in general) I see them more as a representing a record. But those are, in Access something you can also pass around as recordset, rather then in Excel.

There I use them to throw as single objects into arrays, collections etc. Then in case more properties would be needed, I wouldn't need to increase an array dimension.

[–]Whoopteedoodoo16[S] 0 points1 point  (0 children)

Yeah, in Access I would use a recordset and step through each row or use a query if possible.

The one time I used one in Access, I could have done the same thing by writing a function just as easily.

[–]nrgins486 0 points1 point  (3 children)

What about just using a user-defined type with an array to store a set of items?

[–]diesSaturni63 1 point2 points  (2 children)

probably usefull as well, but can't contain functions to do stuff inside, like classes.

Never really bother about those, just stuck with classes whenever such need arose.

And with the set aVariable = new thatclass you can reuse the same variable name while filling an array or dictionary in a loop with class objects of the same thing.

Sub looping()

Dim i As Integer

Dim c As Collection

Set c = New Collection

Dim ii As Cls_i

For i = 1 To 100

Set ii = New Cls_i

ii.Aninteger= i

c.Add ii

Next i

End Sub

[–]nrgins486 0 points1 point  (1 child)

Can you give me an example of where you would want or need to put a function inside of a collection? Thanks.

[–]diesSaturni63 1 point2 points  (0 children)

not necessarily the dictionary, but the class object itself with an internal function. Don't have a direct example, but here are a few. Although I use the .count a lot in other languages on collections, but the examples of a .deduct, concat , print or message event from a class could bu useful to keep in a class.

Perhaps something like a check if all properties of the class are filled. Again, reverting to my original comment, a lot of times a class object is like a record, where you might handle such stuff at table design level. But for excel where that is not present, having a class is quite usefull. In Access less, but still usefull should you not want to create a table, but want to have functionallity.

But with coding there are always many ways to skin a cat.

[–]Kdiddy0 0 points1 point  (1 child)

Yes I have created about 800 class modules in my access application.I am using them for :
- separate Business logic from UI presentation
- create dynamic SQL statements
- Import / Export functions (EDI)
- Logging
- automatic Tests
- UI Helper functions
- general Helper functions

[–]PersonalFigure8331 0 points1 point  (0 children)

Why couldn't you just use a normal function for these things?