Public Statement

Description

Used at module level to declare public variables and allocate storage space.

Syntax

Public varname[([subscripts])][As [New] type][, varname[([subscripts])][As[New] type]] . . .

The Public statement syntax has these parts:

Part

Description

varname

Name of the variable; follows standard variable naming conventions.

subscripts

Dimensions of an array variable; up to 60 multiple dimensions may be declared. The argument subscripts uses the following syntax:

[lower To] upper [,[lower To] upper] . . .

When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.

New

Keyword used to indicate that a declared object variable is a new instance of a Visual Basic object or an externally creatable OLE Automation object. The New keyword can’t be used to create variables of any intrinsic data type and can’t be used to create dependent OLE Automation objects.

type

Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined.


Remarks

Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.

Caution The Public statement can’t be used in a class module to declare a fixed-length string variable.

Use the Public statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer:


Public NumberOfEmployees As Integer

Also use a Public statement to declare the object type of a variable. The following declares a variable for a new instance of a worksheet.


Public X As New Worksheet

If the New keyword is not used when declaring an object variable, no instance of the object actually exists. A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it does not refer to any particular instance of an object.

You can also use the Public statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.

If you do not specify a data type or object type and there is no Deftype statement in the module, the variable is Variant by default.

When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string, and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it were a separate variable.

Tip When you use the Public statement in a procedure, it is a generally accepted programming practice to put the Public statement at the beginning of the procedure.

See Also

Array Function, Const Statement, Dim Statement, Option Base Statement, Option Private Statement, Private Statement, Property Get Statement, Property Let Statement, Property Set Statement, ReDim Statement, Set Statement, Static Statement, Type Statement.

Specifics (Microsoft Access)

By default, module-level variables are private to that module. You must explicitly declare a variable as public with the Public statement.

In a standard module, module-level variables declared with the Public statement are available to all other procedures in all modules in all Microsoft Access databases. However, they are not visible in the Object Browser, and they are not available to any other applications other than Microsoft Access.

If a public variable is declared in a standard module that contains the Option Private statement, the variable is available to all procedures in the current database, but not to procedures in other databases.

A public variable declared in a form module or report module is visible to all procedures in the current database, but not to procedures in other databases. To refer to a public variable declared in a form or report module from another module, you must qualify the variable with the class name of the form or report.

For example, if you declare the public variable varPublic in the module of a form called OrderForm, you can refer to that variable from a standard module as follows.


Form_OrderForm.varPublic

Note The Public statement can’t be used in a form or report module to declare a fixed-length string or an array.

Example

This example uses the Public statement at the module level (General section) of a standard module to explicitly declare variables as public; that is, they are available to all procedures in all modules in all applications unless Option Private Module is in effect.


Public Number As Integer                        ' Public Integer variable.NameArray(1 To 5) As String            ' Public array variable.
' Multiple declarations, two Variants and one Integer, all Public.MyVar, YourVar, ThisVar As Integer

The following example uses the Public statement at the module level to explicitly declare variables as public. Public variables are available to all procedures in all modules within the current database. If they are declared in a standard module, then they are also available to a referencing database.


Public intNum As IntegerobjApplication As Object