Global variable is losing its value

In this access form I'm working on, I have a global variable that takes a value from another form in its Form_Load event. For some reason, an unknown variable "loses its value" (becomes = ") after some time or some kind of event occurs. I could not notice anything special that causes this behavior. Are global variables reset after some time "inaction" in the form?

This is how I set the global variables I'm 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 
+7
vba ms-access ms-access-2000
source share
5 answers

An alternative solution (only from 2007 onwards), I began to use TempVars instead of global variables in an odd situation, in which I "needed" something global. This is a collection and it is stored for the entire duration of the application, if you have not explicitly released it. Therefore, in some cases, I feel that it is 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 

A quick search should show you more links to resources, but I have included a link to the base

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

+7
source share

I hope visitors see this post as it provides an important additional detail.

Even if you declare a variable globally, it seems that if you set this variable value in the form module, this value is lost when the form is unloaded.

The solution (in my case) was as simple as replacing:

Unload me

... from...

Me.Hide

The variables (and objects) that I set in this code module then stored their values ​​for the lifetime of the application instance.

+3
source share

You can create a "fake" global variable

  • form creation (e.g. frmGlobal name)
  • make sure the form is always open but hidden
  • create a TextBox for each required global variable (e.g. tVar1)
  • in your code, a link such as Form_frmGlobal.tVar1

The disadvantage is that an unrelated text field may not give you the specific data type you want

In two ways it

  • in your code, explicitly convert the text field to a data type when referencing a global variable e.g. Clng (Form_frmGlobal.tVar1)

  • another option is to create a table with one row and bind your text fields in hidden form to the table so that your data types are enforced

You can use the bonus of this method for permanent storage between Caveat sessions: make sure that this table is local to only one user in the foreground database file (do not want to put it in the database in the background due to multi-user rewriting of each other). It is assumed that you are using separate databases with front-end and rear-end interfaces, with front-end distribution to each user workstation.

+1
source share

This may help: https://accessexperts.com/blog/2011/01/12/multi-session-global-variables/ Juan Soto explains how to use a local table to store variables and how to call them when necessary. It can serve your purpose in 2000, as TempVars is not an option. You can always remove the "on close" database variables so that the UID and PWD are not saved.

+1
source share

I see nothing in this statement that tells me that it is a global variable. You set global variables above ALL Subs / Functions and below the Compare Compare statement in the module, specifying:

 PUBLIC X as string 

Any other variable is only good until the completion of the sub or function.

In addition, global variables MUST be declared in PROPER MODULE. You cannot declare them in the form module.

0
source share

All Articles