By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

Module and Class Module

100+
P: 553
Hi

whats the difference b/w Module and Class module. Why you would use class module or the other one ?

Thanks
Jul 16 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 634
Hi
Hi

whats the difference b/w Module and Class module. Why you would use class module or the other one ?

Thanks
How long have you got ?

This is wjat Access Help has to say for the "Module Object".

A Module object refers to a standard module (standard module: A module in which you can place Sub and Function procedures that you want to be available to other procedures throughout your database.) or a class module (class module: A module that can contain the definition for a new object. When you create a new instance of a class, you create the new object. Procedures defined in the module become the properties and methods of the object.).
Does that make any sense?
Any point you don't understand then post any specific questions.

I personally usually only use Classes if I need an associated Method(s) ie it does something or a validation (True or False), otherwise I tend to use User Defined Type variables, which are very handy, and can be assigned to an array so eliminating multi-dimensional arrays (which can be a problem if you want to use the Redim/Preserve on more than one dimension.

This however is normally in Excel. I have only used one object/class in Access.

Of course, this can all be achieved by other means subs/functions etc. but object are so much easier to use, mantain, scale etc. once defined.

Sorry about the ramble.

MTB
Jul 16 '07 #2

100+
P: 553
Hi MTB

For Class module, we create an object to call any Sub or Functions but we make an object for normal module as well - is that not right. if true then what would be the difference in both left?

as you say best is to use properties, pre-defined functions, etc

thats what i think.. and even if we need to call some Subs or Functions, we can write them on same file (or window) where we write other code - such as onBtn_Click() etc.... so why put the Subs/functions in a seperate file/window.

Can you give me an example why would you use class module and the simple module?

Thanks
Hi

How long have you got ?

This is wjat Access Help has to say for the "Module Object".


Does that make any sense?
Any point you don't understand then post any specific questions.

I personally usually only use Classes if I need an associated Method(s) ie it does something or a validation (True or False), otherwise I tend to use User Defined Type variables, which are very handy, and can be assigned to an array so eliminating multi-dimensional arrays (which can be a problem if you want to use the Redim/Preserve on more than one dimension.

This however is normally in Excel. I have only used one object/class in Access.

Of course, this can all be achieved by other means subs/functions etc. but object are so much easier to use, mantain, scale etc. once defined.

Sorry about the ramble.

MTB
Jul 16 '07 #3

JKing
Expert 100+
P: 1,206
Hi, I'll try to make this as simple as I can. The Microsoft help does a fairly good job in explaining the two in a concise manner. I'll attempt to broaden their explanation and provide an example for each case.

To really grasp the concept you need to understand scope, objects, classes and few other key concepts like inheritance, polymorphism and encapsulation.

Module
A module is used to declare Subs or Functions that will be available throughout your project.

Why would you create a module instead of writing your Sub or Function below your form code?

Sometimes you will find that you need to use that Sub or Function in several forms. In this case the Sub/Function you declared will only be available to the form you declared it in. This means you would have to write the same code for the Sub/Function on every form you wish to use it. To get around this you create modules. They help reduce repetition and help to organize your code.

An example would be any complex math functions you need to use in more than one form. Or something simpler such as formatting a block of strings.

Class Modules
A class module is used to define objects. Defining an object means creating properties and methods specific to that object. A class itself does nothing. It is not until an object of the class is declared and instantiated that the work really happens.

When would I ever need to use classes and objects?

Learning object oriented programming and wrapping your head around the concept can be confusing and overwhelming at first. Learning to plan your coding around objects can also be difficult.

Creating classes is similar to creating tables of a normalized database. Within a table you store the attributes of the entity the table represents. In a class you also store the attributes of the object the class is representing. However these attributes are extended to not only data but to actions that can be performed by those objects.

Let's imagine we are creating a class to represent a Bank Account object. Every bank account has some things in common. They all have an owner whether it be a person or a company, a PIN, home branch, balance and they all allow deposits and withdrawls.

Within in our BankAccount class we would create properties for owner, pin, home branch and balance. Properties allow us to get or set variables of the class. We would also create methods for the class. These would be our methods for withdrawing and depositing money. The deposit method would increase the balance and the withdraw would decrease the balance. Perhaps for added functionality we would like a Print method that returns a formatted string displaying the users banking info. We would add that here too.

Now with a BankAccount class that describes how a BankAccount object would behave we can start creating BankAccount objects to be used in our code.
Jul 16 '07 #4

100+
P: 553
MTB

Thanks for the explanation. It really has made my concept clear.

Regards


Hi, I'll try to make this as simple as I can. The Microsoft help does a fairly good job in explaining the two in a concise manner. I'll attempt to broaden their explanation and provide an example for each case.

To really grasp the concept you need to understand scope, objects, classes and few other key concepts like inheritance, polymorphism and encapsulation.

Module
A module is used to declare Subs or Functions that will be available throughout your project.

Why would you create a module instead of writing your Sub or Function below your form code?

Sometimes you will find that you need to use that Sub or Function in several forms. In this case the Sub/Function you declared will only be available to the form you declared it in. This means you would have to write the same code for the Sub/Function on every form you wish to use it. To get around this you create modules. They help reduce repetition and help to organize your code.

An example would be any complex math functions you need to use in more than one form. Or something simpler such as formatting a block of strings.

Class Modules
A class module is used to define objects. Defining an object means creating properties and methods specific to that object. A class itself does nothing. It is not until an object of the class is declared and instantiated that the work really happens.

When would I ever need to use classes and objects?

Learning object oriented programming and wrapping your head around the concept can be confusing and overwhelming at first. Learning to plan your coding around objects can also be difficult.

Creating classes is similar to creating tables of a normalized database. Within a table you store the attributes of the entity the table represents. In a class you also store the attributes of the object the class is representing. However these attributes are extended to not only data but to actions that can be performed by those objects.

Let's imagine we are creating a class to represent a Bank Account object. Every bank account has some things in common. They all have an owner whether it be a person or a company, a PIN, home branch, balance and they all allow deposits and withdrawls.

Within in our BankAccount class we would create properties for owner, pin, home branch and balance. Properties allow us to get or set variables of the class. We would also create methods for the class. These would be our methods for withdrawing and depositing money. The deposit method would increase the balance and the withdraw would decrease the balance. Perhaps for added functionality we would like a Print method that returns a formatted string displaying the users banking info. We would add that here too.

Now with a BankAccount class that describes how a BankAccount object would behave we can start creating BankAccount objects to be used in our code.
Jul 17 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.