MS Access, Class Objects, Modules, Class Modules

If I want to add function, which one should I choose to add?
Hiroyuki TamuraField EngineerAsked:
Who is Participating?
I wear a lot of hats...



for a normal function

insert in a Module


Experts Exchange Solution brought to you by

Your issues matter to us.


Start your 7-day free trial
Hiroyuki TamuraField EngineerAuthor Commented:
thank you, Rgonzo1971. what is the difference?
Dale FyeOwner, Dev-Soln LLCCommented:
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.
Active Protection takes the fight to cryptojacking


Paul Cook-GilesSenior Application DeveloperCommented:
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!
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 EngineerAuthor Commented:
Thank you, All
Microsoft Access

From novice to tech pro — start learning today.