vba 全局变量失去价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17601919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:10:39  来源:igfitidea点击:

Global variable loses its value

vbams-accessms-access-2000

提问by ApplePie

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 ?

在我正在处理的这个 Access 表单上,我有一个全局变量,它从另一个表单的 Form_Load 事件中获取其值。由于某种原因,我不知道变量在经过一段时间或发生某些事件后“失去了它的价值”(变成 =“”)。我还没有注意到任何特别会触发这种行为的东西。在表单上“不活动”一段时间后是否会重置全局变量?

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

回答by ashareef

An alternate solution (Only 2007 and onwards) I've started using is TempVarsinstead 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.

我开始使用的另一种解决方案(仅 2007 年及以后)是TempVars在我“需要”全局的奇怪情况下而不是全局变量。它是一个集合,除非您明确释放它,否则它会在应用程序的整个过程中持续存在。所以在某些情况下,我觉得它比全局变量更有用,在某些情况下更糟。

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

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

回答by John Joseph

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.

我在该代码模块中设置的变量(和对象)然后在应用程序实例的整个生命周期中保留它们的值。

回答by LauraNorth

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.

这可能会有所帮助:https: //accessexperts.com/blog/2011/01/12/multi-session-global-variables/Juan Soto 解释了如何使用本地表来保存变量以及如何在需要时调用它们。它可能在 2000 年满足您的目的,因为 TempVars 不是一个选项。您可以随时删除数据库“关闭时”的变量,以便不保留 UID 和 PWD。

回答by Mick Merlin

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
  • 创建一个表单(例如名为 frmGlobal)
  • 确保表单始终打开但隐藏
  • 为您想要的每个全局变量创建一个 TextBox(例如 tVar1)
  • 在您的代码中,参考例如 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

  • 在您的代码中,在引用全局变量时显式地将文本框转换为数据类型,例如 Clng(Form_frmGlobal.tVar1)

  • 另一种选择是创建一个单行表并将隐藏表单上的文本框绑定到该表,以便强制执行您的数据类型

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.

这种方法的一个好处是你可以在会话之间使用持久存储警告:确保这个表只对单个用户本地,在前端数据库文件中(不想把它放在后端数据库中,因为有多个- 用户相互覆盖)。这假设您使用前端 + 后端分离的数据库,并将前端分配到每个用户的工作站。

回答by Johnny Bones

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.

任何其他变量仅在 sub 或 function 完成之前有效。

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

此外,必须在 PROPER MODULE 上声明全局变量。您不能在表单的模块上声明它们。