MWE
08-23-2005, 11:09 AM
Per VBA Help regarding the Static statement:
Used at procedure level to declare variables and allocate storage space. Variables declared with the Static statement retain their values as long as the code is running.
From this I would conclude that once the code stops running, variable values are "lost" and if the code is run again, static variables are reset to "zero". But that does not seem to be the case. Consider this test example:
Sub TestStatic()
Dim I As Integer
Dim J As Integer
For I = 1 To 5
Call StaticSub(J)
Next I
MsgBox J
End Sub
Sub StaticSub(J)
Static Counter As Integer
Counter = Counter + 1
J = Counter
End Sub
The first time I ran TestStatic, the display was "5". The second time it was "10" and so on. Counter is not resetting even though the code HAS stopped running. If I force an error leading to an abnormal end, the next time I run TestStatic, the display is again 5.
So how do I force Counter back to zero each time I run the parent application?
Used at procedure level to declare variables and allocate storage space. Variables declared with the Static statement retain their values as long as the code is running.
From this I would conclude that once the code stops running, variable values are "lost" and if the code is run again, static variables are reset to "zero". But that does not seem to be the case. Consider this test example:
Sub TestStatic()
Dim I As Integer
Dim J As Integer
For I = 1 To 5
Call StaticSub(J)
Next I
MsgBox J
End Sub
Sub StaticSub(J)
Static Counter As Integer
Counter = Counter + 1
J = Counter
End Sub
The first time I ran TestStatic, the display was "5". The second time it was "10" and so on. Counter is not resetting even though the code HAS stopped running. If I force an error leading to an abnormal end, the next time I run TestStatic, the display is again 5.
So how do I force Counter back to zero each time I run the parent application?