A code moduleor standard module,unlike the familiar form module, does
not have a GUI and can contain only code.
Procedures the programmer wishes to use in multiple projects are often placed in code
modules.
Code modules are created by selecting Add Module from the Project menu.
Code modules are maintained in a separate folder in the Project explorer window, as
shown below:
Code modules do not have a GUI, so the View Object button is always grayed.
Like a form module, a code module has a general declaration which can hold variables,
Option Explicit, Enums, etc.
Procedures are added using the techniques described earlier. Code module
file names end in .bas.
A project can have multiple code modules (as well as multiple form modules).
Private vs. Public
Inorder to use code modules effectively, the programmer must understand scoping
rules.
By default, module variables and event procedures are Private to the module in
which they are defined, which means that they can only be used within that module.
If the programmer wishes to allow another module to use a variable or procedure, keyword
Public is used in the declaration.
Public variables and procedures are accessible to every module in the project.
Procedures that other modules are expected to use are made Public by the programmer and
utility procedures are generally made Private.
Example
The program below contains one form module and one code module.
Clicking the form's Print button calls the code module's programmer-defined Public Sub
procedure ModulePrint, which displays text.