A Class module contains the definition for a new object. Procedures to represent the properties and methods of the object can be defined in the module. Class modules were available to Access developers first in the Access 97 database. The addition of standalone class modules in Visual Basic for Applications or VBA is a welcome site to users and would be developers. A class is the definition of an object. The module allows you to define...
the class or object. Once a class is defined, you can then create a new object simply by creating a new instance of the class.
While you may not be aware of it, you have probably used classes and associated objects already. As a brief example, the forms or reports modules you use are class modules, while the form or report itself is actually the instantiated object. Instantiated is when you create a new instance of a class, you create a new object, usually by using the set keyword, which assigns a variable to represent the new object. That is also a new occurrence of a class. Another good example of object definition and its instantiated result is the ComboBox class and a ComboBox object on your generated form.
Many times the same set of processes must be applied repeatedly whenever certain types of data are needed by your application. You can encapsulate your data combined with the rules and regulations that apply, and let less experienced developers have access to it without destroying or corrupting it. Since they are allowed only to reference the properties and methods, they do not go through entire pages of your code. Encapsulation preserves the integrity of your program.
Some of the best reasons for working with class modules are:
- If you use the same types of forms, you can view multiple forms in separate windows.
- If you have a multiuser environment and want to speed it up, you can create class modules that represent your records as reusable objects.
- You can ensure that business rules are enforced, by encapsulating them and associated data into a single class.
More information about creating class modules is available at http://www.microsoft.com/officedev/articles/movs109.htm.
About the Author
Barrie Sosinsky (firstname.lastname@example.org) is president of consulting company Sosinsky and Associates (Medfield MA). He has written extensively on a variety of computer topics. His company specializes in custom software (database and Web related), training and technical documentation.
For More Information
- What do you think about this tip? E-mail us at editor@searchDatabase.com with your feedback.
- The Best VB Web Links: tips, tutorials, scripts, and more.
- Have an VB tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
- Ask your technical VB questions--or help out your peers by answering them--in our live discussion forums.
- Check out our Ask the Experts feature: Our SQL, Database Design, Oracle, SQL Server, and DB2 gurus are waiting to answer your toughest questions.