See Also
A general procedure tells the application how to perform a specific task. Once the procedure is defined, it must be explicitly invoked by the application . By contrast, an event procedure remains idle until called upon to respond to events caused by the user or triggered by the system. You can place a general procedure in either form code or in a module . General procedures in a form can be accessed only from within that form; general procedures in a module can be accessed from any form or module in your application. Using general procedures can help divide complex application code into more manageable units.
To create a general procedure
1. Open the Code window for the form or module for which you want to write the procedure.
2. From the View menu, choose New Procedure.
3. In the text box, type a name for the procedure.
4. Select Sub to create a Sub procedure or Function to create a Function procedure .
Note that a Function procedure returns a value and a Sub procedure does not.
5. Choose OK.
Visual Basic sets up a template so you can enter code for the general procedure.
Note You can also create a new procedure by typing Sub ProcedureName or Function ProcedureName in the Code window.
Syntax
Use this general syntax when writing a Sub procedure:
Sub ProcedureName (arguments)
local variable and constant declarations
statements
End Sub
Use this syntax when writing a Function procedure:
Function ProcedureName (arguments) As datatype
local variable and constant declarations
statements
ProcedureName = return value
End Function
Use general procedures for:
Sharing code among controls on one form. Write a general procedure in form code and attach an event procedure to each control that invokes the general procedure.
Sharing code among multiple forms. Create a module and write the shared code there as a general procedure. For each form or control you want to share the code, attach an event procedure that calls the general procedure.
If you have too much code in a module you may exceed memory limitations. It may help if you create one or more modules and offload code by attaching to the form or its controls short event procedures that call general procedures in the module(s). Place global declarations in any module.
Multiple-form application startup. Create a module and write a general procedure called Main for your startup routine.
Help:
Guidelines for Entering and Editing Code
Programmer's Guide:
Chapter 6, "Programming Fundamentals"
Chapter 7, "Variables, Constants, and Data Types""