Statics and Constants in VB6


Written By TheVBProgramer.

Statics in Visual Basic

Statics allow you to maintain a variables value even when it goes out of scope. For example:

Suppose you have a Sub in a form called CountThem that looks like this:


Private Sub CountThem()


Dim intI As Integer

Static intJ As Integer

intI = intI + 1

intJ = intJ + 1

Print intI, intJ


End Sub


Suppose you have some other Sub that calls CountThem three times in a row:


Call CountThem

Call CountThem

Call CountThem


The following output would be displayed on the form (by default, the "Print" statement directs its output to the current form):


(values for intI)

(values for intJ)








Note that the "regular" variable, intI, declared with "Dim", does not retain its value between calls, whereas the Static variable, intJ, does.


Note: The keyword "Static" can also be used in the Sub procedure header, which causes all variables in that procedure to be static. Example:


Private Static Sub AllVarsAreStatic()

Dim intCounter As Integer ' as if declared Static

Dim strErrMsg As String ' as if declared Static

. . . ' other statements

End Sub



VB supports the use of symbolic, or named constants. Constants are similar to variables, except that you provide a value for the constant when you declare it, and its value can never change. The syntax for declaring a constant is:


[Public | Global | Private] Const constantname [As datatype] = expression


A global (project-level) constant can only be declared in a standard (.bas) module (not a form), using "Public Const" or "Global Const" (the "Public" keyword is preferred). Module-level constants can be declared in the General Declarations Section of either a standard or form code module using "Private Const" (or just "Const"; the default is "Private"). Local-level constants are declared in any procedure of a standard or form module just using the word "Const" (no "Public" or "Private"). If you omit the "As datatype" clause, VB will use its "best guess" as to what the datatype should be, based on the expression.


The following table shows how constants may be declared and the location of their declaration affects the scope:



Keyword Used to Declare the



Where Declared



General Declarations Section of a Form (.frm) Module


General Declarations Section of a Standard (.bas) Module


Sub or Function procedure of a Form or Standard Module


module-level scope

module-level scope

local-level scope


Private Const

module-level scope

module-level scope

not allowed


Public Const

-or –

Global Const


not allowed


project-level scope


not allowed



Sample Constant Declarations:


Public Const gsngTAX_RATE As Single = 0.06


Private Const mdtmCUT_OFF_DATE As Date = #1/1/1980#


Const strERROR_MESSAGE = "Invalid Data" 'String data type assumed


Note that in Const declarations, string literals are delimited with double quotes ("), date literals are delimited with pound signs (#), and numeric literals are not delimited.


Naming Conventions for Constants


Similar to naming variables, use the lowercase three-character datatype prefix ("int", "str", etc.), prefixed by an "m" if a module-level constant or "g" if a global (project-level) constant. However, for the main part of the name, use all capital letters (with underscores to break up individual words within the name).

If you enjoyed this post, subscribe for updates (it's free)

static variables with initialisation value in VB6

Well to bad VB6 don't support static variables with initialisation like

Static isFirstTime as boolean = true

Well that's how you may simulate this behaviour:

Static isFirstTime: If isFirstTime = Empty Then _
       isFirstTime = True

How would you incorporate

How would you incorporate this into the keyascii of the arrow keys