Structuring Your Code

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

At the application level, your code is contained in one or more standard modules or class modules and in modules behind forms, reports, or documents. You apply structure to your code at this level by organizing your code logically within these components in your application. Within any module, the procedures should have some relation to each other. For example, you could keep all data access code in a single module. Form, report, or document modules should contain only code that applies directly to the form, report, or document or to controls it contains.

At the procedure level, applying a structure to the code means breaking up large procedures into smaller ones and using line breaks, white space, and indentation to organize and illustrate the logical structure of the code. Any general-purpose procedures called by code in these objects should be contained in a separate module. In addition, you should add comments at the module level to provide information on the nature and purpose of the procedures contained in the module.

You should use these principles whether you are writing Microsoft® Visual Basic® for Applications (VBA) code or script in an HTML page. You can think of an HTML page as being similar to a VBA application for the purposes of structuring your script. You can think of blocks of script within <SCRIPT> tags as being similar to VBA procedures contained in a module. The script and procedures used between <SCRIPT> tags should be related to other script within the same set of tags. Any general-purpose procedures called from the script in an HTML page should be grouped together within their own pair of <SCRIPT> tags or kept in a scriptlet.

See Also

Structuring and Formatting Your Code | Formatting Code | Using Web Technologies