Global variable loses its value

ApplePie picture ApplePie · Jul 11, 2013 · Viewed 17.5k times · Source

On this Access form I am working on I have a global variable that take its value from another form on its Form_Load event. For some reason unknown to me the variable "loses its value" (becomes = "") after some time elapses or some event occurs. I have not been able to notice anything in particular that triggers this behaviour. Are global variables reset after some time of "inactivity" on the form ?

Here is how I set the global variables I am talking about:

Private Sub Form_Load()       
    '...
    Set prev_form = Form_Identification.Form
    PasswordSybase = prev_form.Password.Value & vbNullString
    UserSybase = prev_form.UserID.Value & vbNullString
    '...
End Sub

Answer

ashareef picture ashareef · Jul 12, 2013

An alternate solution (Only 2007 and onwards) I've started using is TempVars instead of globals in the odd situation I "needed" something global. It's a collection and it persists for the duration of the application unless you explicitly release it. So in some cases I feel its more useful than globals and in some cases worse.

TempVars.Add myVarName, myVarValue ' To initialize
TempVars.Item(myVarName) = newVarValue ' To reference and assign a new value
TempVars.Remove(myVarName) ' To release

Quick search should show you more lot references, but I've included link to a basic one

http://blogs.office.com/b/microsoft-access/archive/2010/09/27/power-tip-maximize-the-user-of-tempvars-in-access-2007-and-2010.aspx