Global variable loses its value

做~自己de王妃 提交于 2019-12-01 16:21:18

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

I do hope that visitors see this post, as it provides an important additional piece.

Even if you declare a variable globally, it appears that - in the event that you set that variable's value in a form module - that value is lost when you UNLOAD the form.

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

Unload Me

...with...

Me.Hide

The variables (and objects) that I set in that code module then retained their values through the entire lifetime of the application instance.

This may help: https://accessexperts.com/blog/2011/01/12/multi-session-global-variables/ Juan Soto explains how to use a local table to keep variables and how to call them when needed. It may serve your purpose in 2000 since TempVars isn't an option. You could always delete the variables "on close" of the database so that UID and PWD aren't kept.

You can create a "fake" global variable by

  • creating a form (e.g. named frmGlobal)
  • make sure the form is always open but hidden
  • create a TextBox for each global variable you want (e.g. tVar1)
  • in your code, reference as e.g. Form_frmGlobal.tVar1

The disadvantage is that an unbound text box may not give you a specific data type you want

The two ways around that are

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

  • another option is create a one-row table and bind your textboxes on your hidden form to the table, so your data types are enforced

A bonus of this method is you can use for persistent storage between sessions Caveat: make sure this table is local to a single user only, in the front end database file (don't want to put it in the back end database because of multi-users over-writing each other). This assumes you are using front end + back end separated databases, with distribution of front end to each user's workstation.

I see nothing in that statement that tells me it's a global variable. You set global variables above ALL Subs/Functions and below an Options Compare statement in a module, by stating:

PUBLIC X as string

Any other variable is only good until the sub or function has completed.

Also, Global variables MUST be declared on a PROPER MODULE. You can't declare them on a form's module.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!