Creating a General Procedure

See Also2EQB7TC

A general procedure tells the application how to perform a specific task.  Once the procedure is defined, it must be explicitly invoked by the application141RJVK.  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 code1FG60U9 or in a module2JU32VW.  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 procedureQDBVHN or Function to create a Function procedureK6LBMC.

     Note that a Function procedure returns a value and a Sub procedure does not.

   5. Choose OK.

     Visual Basic sets up a template1KTWQTC 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.



Use this general syntax when writing a Sub procedure:

Sub ProcedureName (arguments)

          local variable and constant declarations


End Sub


Use this syntax when writing a Function procedure:

Function ProcedureName (arguments) As datatype

          local variable and constant declarations


          ProcedureName = return value

End Function


Use general procedures for:

         Sharing code among controls on one form.  Write a general procedure in form code and attach2LBGK4C 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.

See Also


Guidelines for Entering and Editing Code5SZA0GX


Programmer's Guide:

Chapter 6, "Programming Fundamentals"

Chapter 7, "Variables, Constants, and Data Types""