如何强制 VBA/Access 要求定义变量?

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

How do I force VBA/Access to require variables to be defined?

ms-accessvba

提问by BIBD

I'm making some significant changes to some VBA code, and some variables are being deleted and/or renamed. It would be a lot easier to find all the locations I need to update if the compiler would complain to me that the variables don't exist instead of creating it on the fly.

我正在对某些 VBA 代码进行一些重大更改,并且正在删除和/或重命名某些变量。如果编译器向我抱怨变量不存在而不是即时创建它,那么找到我需要更新的所有位置会容易得多。

How do I force VBA/Access to require variables to be declared?

如何强制 VBA/Access 要求声明变量?

回答by Tony Toews

You need to use Option Explicitat the top of each VBA code module including forms and reports.

您需要Option Explicit在每个 VBA 代码模块的顶部使用包括窗体和报表。

You can set this for all future created modules and VBA code behind forms and reports by going into the VBA editor >> Tools >> Options >> Editor tab and ensuring Require Variable Declaration is checked.

您可以通过进入 VBA 编辑器 >> 工具 >> 选项 >> 编辑器选项卡并确保选中需要变量声明,为所有未来创建的模块和表单和报告背后的 VBA 代码设置此项。

From Access 2003 help:

来自 Access 2003 的帮助:

Require Variable Declaration — Determines whether explicit variable declarations are required in modules. Selecting this adds the Option Explicitstatement to general declarations in any new module.

需要变量声明——确定模块中是否需要显式变量声明。选择此项会将Option Explicit语句添加到任何新模块中的一般声明中。

I also use camel case when I Dim my variables. ThisIsAnExampleOfCamelCase. As soon as I exit the VBA code line if Access doesn't change the lower case variable to camel case then I know I've got a typo.

当我调暗我的变量时,我也使用驼峰式大小写。 ThisIsAnExampleOfCamelCase. 一旦我退出 VBA 代码行,如果 Access 没有将小写变量更改为驼峰大小写,那么我就知道我有一个错字。

回答by David-W-Fenton

Some History on OPTION EXPLICIT and Access VBA

关于 OPTION EXPLICIT 和 Access VBA 的一些历史

To follow on from Tony's answer, here's some explanation of why there are issues with OPTION EXPLICIT not being on in some Access code modules.

为了继续托尼的回答,这里有一些关于为什么在某些访问代码模块中没有打开 OPTION EXPLICIT 的问题的一些解释。

In Access 95 and Access 97 (the first two Office versions with VBA), Access had a different code editor than the other office programs. In Access 2000, Microsoft implemented the VBE from the other Office apps in Access. At the same time, MS chose to make Access VBA modules behave like the modules in the other apps, which defaulted to nothaving OPTION EXPLICIT.

在 Access 95 和 Access 97(带有 VBA 的前两个 Office 版本)中,Access 的代码编辑器与其他办公程序不同。在 Access 2000 中,Microsoft 从 Access 中的其他 Office 应用程序实现了 VBE。同时,MS 选择让 Access VBA 模块的行为类似于其他应用程序中的模块,默认情况下没有OPTION EXPLICIT。

Thus, in Access 2000, by default, modules were created without OPTION EXPLICIT.

因此,在 Access 2000 中,默认情况下,模块是在没有 OPTION EXPLICIT 的情况下创建的。

This was, of course, a really stupid design decision on MS's part, and they reversed it later (I can't remember if it was Access 2002 or 2003 that rectified the problem and defaulted to OPTION EXPLICIT in all new modules again). The reason it was dumb (and MS should have known this) is because Access is a DATABASE APPLICATION DEVELOPMENT tool, and thus is operating on data that is strongly typed. Thus, the code environment should be strongly typed by default so that it is in harmony with the data it is working with.

当然,这对 MS 来说是一个非常愚蠢的设计决定,后来他们将其撤消(我不记得是 Access 2002 还是 2003 纠正了问题并再次在所有新模块中默认为 OPTION EXPLICIT)。它愚蠢的原因(MS 应该知道这一点)是因为 Access 是一个数据库应用程序开发工具,因此对强类型的数据进行操作。因此,默认情况下代码环境应该是强类型的,以便它与它正在使用的数据保持一致。

In Excel or Word, the data is notstrongly typed, and it thus makes more sense to use variant data types for just about everything, simply to make it easier for everyone. The downside of implementing that by not using OPTION EXPLICIT is that you can end up with typos that automatically intrdoduce new variables [such as the variable "intrdoduce" -- if I was writing this post with OPTION EXPLICIT, that wouldn't have happened without producing a runtime error! :)]. This is a problem with all such languages that work this way (I pull my hair out working in PHP, where variable names can be distinct by case, i.e., $Var is not the same variable as $var; but I digress), but MS made the decision to implement it that way in Word and Excel on the theory that the people writing code there are going to have an easier time of it if they aren't forced to declare their variables.

在 Excel 或 Word 中,数据不是强类型,因此对几乎所有内容使用变体数据类型更有意义,只是为了让每个人都更容易。不使用 OPTION EXPLICIT 来实现它的缺点是,你最终可能会出现自动引入新变量的拼写错误 [例如变量“引入”——如果我用 OPTION EXPLICIT 写这篇文章,没有这种情况就不会发生产生运行时错误!:)]。这是所有以这种方式工作的语言的问题(我在 PHP 中工作,其中变量名可以根据大小写不同,即 $Var 与 $var 不是同一个变量;但我离题了),但是MS 决定在 Word 和 Excel 中以这种方式实现它的理论是,如果他们不这样做,那里编写代码的人会更轻松。

So, MS made the mistake of making Access's version of the VBE like the other apps, even though there was no logic internal to Access's own purposes that supported that move. And MS then backed out that change and returned to the previous status quo (i.e., OPTION EXPLICIT by default in all modules).

因此,MS 犯了一个错误,将 Access 版本的 VBE 与其他应用程序一样,即使 Access 自己的目的没有内部逻辑支持该移动。然后 MS 撤消该更改并返回到以前的状态(即,默认情况下所有模块中的 OPTION EXPLICIT)。

Thus, you will often see apps that began life in Access 2000 that have modules all over the place without OPTION EXPLICIT. When I have to work on such an app, my first task is to implement OPTION EXPLICIT in all modules and then fix the damned thing so it will compile (which is often quite tough, given that the thing was programmed without it).

因此,您会经常看到从 Access 2000 开始的应用程序,其模块随处可见,而没有 OPTION EXPLICIT。当我必须在这样的应用程序上工作时,我的第一个任务是在所有模块中实现 OPTION EXPLICIT,然后修复该死的东西以便它可以编译(这通常很困难,因为该东西是在没有它的情况下编程的)。

回答by Lance Roberts

This question was quite helpful for Excel, you might see if it'll work for Access:

这个问题对 Excel 很有帮助,您可能会看到它是否适用于 Access:

Lost Variables

丢失的变量

Essentialy, MZ-Toolswill search through your code and tell you what is not being used. The version for VBA can be found here. Use the Review Source Codefeature.

本质上,MZ-Tools将搜索您的代码并告诉您哪些未被使用。VBA 的版本可以在这里找到。使用查看源代码功能。