本文介绍了如何强制 VBA/Access 要求定义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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/Access 要求声明变量?

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

推荐答案

您需要在每个 VBA 代码模块(包括表单和报表)的顶部使用 Option Explicit.

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

您可以通过进入 VBA 编辑器 >> 工具 >> 选项 >> 编辑器选项卡并确保选中需要变量声明,为所有未来创建的模块和表单和报表背后的 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.

来自 Access 2003 帮助:

From Access 2003 help:

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

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

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.

这篇关于如何强制 VBA/Access 要求定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 20:29