VBA - 全局变量范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19136489/
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 23:35:48  来源:igfitidea点击:

VBA - Global Variable Scope?

vbams-access

提问by Mark Pelletier

I'm struggling here a bit.

我在这里有点挣扎。

I had created a few Public variables on a form (MainForm). I now realize I need to expand the scope of some of these variable to additional forms.

我在表单 (MainForm) 上创建了一些公共变量。我现在意识到我需要将其中一些变量的范围扩展到其他形式。

So, I moved the Public declarations to a new Module called "Globals".

因此,我将 Public 声明移到了一个名为“Globals”的新模块中。

When I attempt to run the new form, I am getting a complaint about an ambiguous variable as I attempt to reset its value to "". No complaint from the "MainForm" using the same variables.

当我尝试运行新表单时,当我尝试将其值重置为“”时,我收到了关于不明确变量的投诉。使用相同变量的“MainForm”没有抱怨。

Module "Globals"

模块“全局”

Public myIP As String
Public myStatus As String

New Form:

新表格:

myStatus = ""   << throws an err

Am I correct to move the Public variable declarations to a Module?

将公共变量声明移动到模块是否正确?

Any thoughts about the "ambiguous variable" err from that new Form??

对那个新表单中的“模糊变量”有什么想法吗??

Thanks!

谢谢!

采纳答案by Aaron Thomas

You may be trying to declare the same variable in the same scope. See herefor more details, also a SO thread related to this here.

您可能试图在同一范围内声明相同的变量。有关更多详细信息,请参阅此处,还有与此相关的 SO 线程此处

As a side note, I notice with Excel VBA you must appropriately qualifya module-level variable if a procedure-level variable is declared with the same name.

作为旁注,我注意到如果使用相同名称声明过程级变量,则使用 Excel VBA 必须适当地限定模块级变量。