本文介绍了如何强制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:

当我将变量变暗时,我也会使用驼峰式大小写. 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:28