MS Access, Class Objects, Modules, Class Modules

Hiroyuki Tamura
Hiroyuki Tamura used Ask the Experts™
If I want to add function, which one should I choose to add?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

for a normal function

insert in a Module

Hiroyuki TamuraField Engineer


thank you, Rgonzo1971. what is the difference?
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
You can add functions in the code behind a form, which is where I normally would put it unless you want to be able to call it from multiple forms, queries, or other code.  In that case, I would put it into a standard code module.
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!

Paul Cook-GilesSenior Application Developer
There are two different types of modules:  "Standard" modules are stand-alone objects you can see in the Navigation pane, and are independent of any other object.  Code (functions or subs) in Standard modules may be called from anywhere in the database.   "Class"  modules are attached to specific form or report.  When you click on one of the events in a form or report's Property window, and either hit the Builder button (the one with three dots), or press Control-F2, you are taken into that object's class module.    Code (functions or subs) in Class modules may be called only from the form or report on which they are dependent.  

Hope that helps!
Most Valuable Expert 2012
Top Expert 2014
Class modules are not necessarily attached to a specific Form or Report (although the module associated with a Form or Report is a Class module).

Classes and Modules are similar, but the usage of them is distinctly different.

A Module contains Subs/Functions, declarations, variables, etc. You can call the Subs/Functions, set the variables, etc. As long as the items are marked as Public, you can use them anywhere in your application without declaring anything else.

A Class module encapsulates an "object" or "entity", and all the functions/methods/properties for that entity. For example, if you're building an Invoicing program you'd have Customers and Invoices (and the Invoices would be broken down into InvoiceHeader and InvoiceDetail). In this situation, you'd have a class to represent your Customer, another to represent an InvoiceHeader, and a third to represent an InvoiceDetail line. Your Customer class could contain a collection of all Open Invoices for the Customer (or all Closed Invoices, or simply All Invoices). You could instantiate multiple Customer classes (if needed), and you could instantiate multiple InvoiceHeader classes (if needed). Each of those would stand on it's own, and could be managed independent of any other like class.

Your Customer Class would contain properties (like Name, Type, Date_Entered, etc), and it would also include functions specific to the Customer object (like CurrentBalance, or NumberOfOpenInvoices). You could NOT call those functions, however, unless you have created (i.e. "instantiated" or "instanced") the Customer object.

Your InvoiceHeader class would contain properties (like InvoiceNumber, InvoiceDate, Customer, etc) and functions specific to the InvoiceHeader, like AddDetailLine, NumDetailLines, etc. Again, you would not be able to call those functions unless you have created the InvoiceHeader object.

You could conceivably create Modules that look somewhat like a Class. You could, for example, create a Customer Module, and fill that Module with the values that define a single Customer. But, if you the need to add a second Customer, you'd have to clear all values from that Module and fill them with the new values. With a class module, you'd just create a new Customer class and fill that new class with the values of the second Customer.
Hiroyuki TamuraField Engineer


Thank you, All

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