Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA™ Plant SCADA

Declaration of Constants

  • Last UpdatedJul 18, 2023
  • 2 minute read

VBA constants can only be declared and referenced within VBA file modules. VBA modular constants have modular scope and cannot be referenced (accessed and used) from outside their VBA module (file).

Note: VBA constants cannot be used directly in Plant SCADA command or expression fields.

Once declared within a VBA module, VBA constants can be referenced and used in any procedure within the same code module. A constant declared outside a procedure has modular scope to all procedures within that same VBA module (file). See Scope of VBA. Constants declared in a Sub or Function procedure have local scope only within that procedure.

VBA constants are declared with the Conststatement in the following format.

Const <ConstantName> [ As <DataType> ] = <expression>

Where:

  • Const is the required constant declaration statement BASIC keyword

  • <ConstantName> represents the required name of the constant being declared

  • <DataType> represents the optional VBA data type of the constant being declared

  • <expression> represents the required value being assigned to the constant

    Note: Do not include the brackets from the explanation in the actual code statement.

If no data type is declared, VBA automatically assigns one of the following data types to the constant:

  • Long (if it is a long or integer).

  • Double (if a decimal place is present).

  • String (if it contains quote marks).

Constant statements can only be declared and assigned using simple expressions. Constants cannot be assigned values from variables, user-defined functions, intrinsic VBA functions (such as Chr), or from any expression that involves an operator. A constant needs to be defined before it can be used.

Example

' Correct declaration examples
Const Seven = 7

' long assignment
Const Pi = 3.14159

' double assignment
Const Lab = "Laboratory"

' string assignment
' Incorrect declaration examples. Note that the following declarations demonstrate incorrect assignments because each contains an operator
Const conPi = 4 * Atn(1)
' will cause a VBA compile error
Const conDegToRad = (conPi / 180)
' will cause a VBA compile error

For an example of using constants in VBA, see Constants.

Note: The use of Global, Public, and Private keywords has no effect on scope in VBA.

See Also

Intrinsic Constants

Variables

VBA Data Types

VBA Function Reference

In This Topic
Related Links
TitleResults for “How to create a CRG?”Also Available in