Sub Statement

Description

Declares the name, arguments, and code that form the body of a Sub procedure.

Syntax

[Private | Public] [Static] Sub name [(arglist)]
[statements]
[Exit Sub]
[statements]End Sub

The Sub statement syntax has these parts:

Part

Description

Public

Indicates that the Sub procedure is accessible to all other procedures in all modules. If used in a private module (one that contains an Option Private statement) the procedure is not available outside the project.

Private

Indicates that the Sub procedure is accessible only to other procedures in the module where it is declared.

Static

Indicates that the Sub procedure’s local variables are preserved between calls. The Static attribute doesn’t affect variables that are declared outside the Sub, even if they are used in the procedure.

name

Name of the Sub; follows standard variable naming conventions.

arglist

List of variables representing arguments that are passed to the Sub procedure when it is called. Multiple variables are separated by commas.

statements

Any group of statements to be executed within the body of the Sub procedure.


The arglist argument has the following syntax and parts:

[Optional][ByVal | ByRef][ParamArray] varname[( )] [As type]

Part

Description

Optional

Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. All Optional arguments must be Variant. Optional can’t be used for any argument if ParamArray is used.

ByVal

Indicates that the argument is passed by value.

ByRef

Indicates that the argument is passed by reference.

ParamArray

Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or Optional.

varname

Name of the variable representing the argument; follows standard variable naming conventions.

type

Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (variable length only), Object, Variant, a user-defined type, or an object type.


Remarks

If not explicitly specified using either Public or Private, Sub procedures are public by default. If Static is not used, the value of local variables is not preserved between calls.

All executable code must be in procedures. You can’t define a Sub procedure inside another Sub, Function, or Property procedure.

The Exit Sub keywords cause an immediate exit from a Sub procedure. Program execution continues with the statement following the statement that called the Sub procedure. Any number of Exit Sub statements can appear anywhere in a Sub procedure.

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can’t be used in an expression.

You call a Sub procedure using the procedure name followed by the argument list. See the Call statement for specific information on how to call Sub procedures.

Caution Sub procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. The Static keyword usually is not used with recursive Sub procedures.

Variables used in Sub procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.

Caution A procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you have defined at the module level has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure, constant or variable, it is assumed that your procedure is referring to that module-level name. Explicitly declare variables to avoid this kind of conflict. You can use an Option Explicit statement to force explicit declaration of variables.

Note You can’t use GoSub, GoTo, or Return to enter or exit a Sub procedure.

See Also

Call Statement, Dim Statement, Function Statement, Option Explicit Statement, Property Get Statement, Property Let Statement, Property Set Statement.

Specifics (Microsoft Access)

In Microsoft Access, a public Sub procedure in a standard module is available to all other procedures in the database and in all other Microsoft Access databases. However, it is not available to any other applications.

If a Sub procedure is declared as public within a private module, then the procedure is available to all other procedures in that database, but is not available to other Microsoft Access databases. Form modules and report modules are always private, as are standard modules that contain an Option Private Module statement.

If you declare a Sub procedure as private in any module, that procedure is available only to other procedures within the same module.

Note If a Sub procedure is available to other Microsoft Access databases, it will be visible from those databases in the Object Browser.

When you create an event procedure for a form or report, Microsoft Access automatically inserts a code stub for a Sub procedure and precedes it with the Private keyword. For example, draw a command button on a new form, set its OnClick property to [Event Procedure], and click on the Build button to view the form’s module. Microsoft Access inserts the following code for you in the module.


Private Sub Command0_Click
Sub

You can then enter the code that you want to execute when that button’s Click event occurs.

Example

This example uses the Sub statement to define the name, arguments, and code that form the body of a Sub procedure.


' Sub procedure definition.SubComputeArea(Length, TheWidth)        ' Sub procedure with two
                                        ' arguments.
    Dim Area As Double                    ' Declare local variable.
    If Length = 0 Or TheWidth = 0 Then    ' If either argument = 0.
        Exit Sub                            ' Exit Sub immediately.
    End If
    Area = Length * TheWidth                ' Calculate area of rectangle.
    Debug.Print Area                        ' Print Area to Debug window.Sub