Global variable resets to zero ??? (1 Viewer)

Partly for this reason I have wrapper functions for tempvars.

Option Compare Database
Option Explicit

Public Function TempVarsBoolean(ByRef VarName As String) As Boolean
TempVarsBoolean = Nz(TempVars(VarName), False)
End Function

Public Function TempVarsDouble(ByRef VarName As String) As Double
TempVarsDouble = Nz(TempVars(VarName), 0)
End Function

Public Function TempVarsDate(ByVal VarName As String) As Date
If IsDate(TempVars(VarName)) Then
TempVarsDate = DateValue(Nz(TempVars(VarName), #1/1/2099#))
Else
TempVarsDate = #1/1/2099#
End If
End Function

Public Function TempVarsLong(ByVal VarName As String) As Long
TempVarsLong = CLng(Nz(TempVars(VarName), 0))
End Function

Public Function TempVarsString(ByVal VarName As String) As String
TempVarsString = CStr(Nz(TempVars(VarName), "*"))
End Function
Very interesting! I'm trying to fully digest the meaning of this now.
 
Are you sure? Or did I miss something?

In a standard module:

Code:
Option Compare Database
Option Explicit

Public Sub Foo()
    Static myStaticVariable As Long
End Sub
                  
Public Sub Bar()
    Debug.Print myStaticVariable ' <-- Compiler raises 'Variable not defined' here
End Sub

From previously referenced VBA Language Reference, section 5.3.1.2, Static Procedures, Runtime Semantics:

All variables declared within the <procedure-body> of a static procedure have module extent.
All variables declared within the <procedure-body> of a non-static procedure have procedure extent.

I hope that clarifies what I said. But the two "All variables..." quotes are cut/paste from the .PDF file in question.
 
Ok, I can follow and agree that.
But before you wrote it would have module scope. ;)
HOWEVER: A variable declared STATIC, even if in the private variable area of a sub or function, has "module" scope.
 

Users who are viewing this thread

Back
Top Bottom