如何有条件地停止访问 VBA 代码?

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

How to stop access VBA code conditionally?

formsms-accessvbaprocedure

提问by KryptKeeper

So I've created a form in Access, which has a combo box and a text box. The form calculates some things after pressing a button but either both the combo box and the text box have to be filled, or neither of them can be filled. What I'd like to do is put in a condition so that if only one of them is filled, a Message Box would popup explaining to fill both boxes and then proceed to stop the code. I understand how to do the message box part, but right now it's showing the message box and then proceeding on with the code which produces an error.

所以我在 Access 中创建了一个表单,它有一个组合框和一个文本框。按下按钮后,表单会计算一些东西,但是组合框和文本框都必须填充,或者它们都不能填充。我想要做的是设置一个条件,如果只有其中一个被填充,则会弹出一个消息框,解释填充两个框,然后继续停止代码。我了解如何执行消息框部分,但现在它正在显示消息框,然后继续执行产生错误的代码。

I've done some research but most of what I'm finding has to do with temporarily pausing the form, not completely stopping it.

我做了一些研究,但我发现的大部分内容与暂时暂停表单有关,而不是完全停止它。

I have a couple of ideas on how to do this, but I'm having trouble executing them and frankly I'm not even sure they'll work. My first plan was to copy and paste all the calculation parts from the procedure, create a new procedure with them, create an if statement in the button clicking procedure and call the new procedure if it passes the if statement. My second plan is a little more tedious but I'm fairly confident it'll work. It's just surrounding the entire code with an if statement, if it passes execute the code, if it doesn't, do nothing and have it reach the end of the procedure and end itself. My only issue with the second one is I have a LOT of code beforehand, and I'd rather not waste a large amount of time re-indenting lines if I can help it, but I have that as a backup if I can't find a better way.

我有一些关于如何做到这一点的想法,但我在执行它们时遇到了麻烦,坦率地说,我什至不确定它们会起作用。我的第一个计划是从程序中复制并粘贴所有计算部分,用它们创建一个新程序,在按钮单击程序中创建一个 if 语句,如果它通过 if 语句,则调用新程序。我的第二个计划有点乏味,但我相当有信心它会奏效。它只是用一个 if 语句包围整个代码,如果它通过则执行代码,如果它不通过,则什么都不做,让它到达过程的末尾并结束自己。我对第二个代码的唯一问题是我事先有很多代码,如果我能帮上忙,我宁愿不要浪费大量时间重新缩进,但如果我不能,我会把它作为备份找到更好的方法。

回答by Tim Lentine

You have a few options available to you:

您有几个选择:

  1. As you suggested, take the code presently tied to the button click event and move it to a new sub procedure. In the button click event perform your validation and if everything passes invoke the sub procedure. If not, show your messagebox.
  2. Assuming you have proper error handling in the button click event, raise an error instead of showing the messagebox. This will move the control flow to your error handler in the procedure.
  3. After displaying the message box, use either Exit Sub or Exit Function to stop code execution.
  1. 正如您所建议的,将当前绑定到按钮单击事件的代码移到一个新的子过程中。在按钮单击事件中执行您的验证,如果一切通过调用子过程。如果没有,请显示您的消息框。
  2. 假设您在按钮单击事件中有正确的错误处理,则引发错误而不是显示消息框。这会将控制流移动到过程中的错误处理程序。
  3. 显示消息框后,使用 Exit Sub 或 Exit Function 停止代码执行。

Option 1 is probably the best approach as it promotes better coding practices. If you have long sub routines it could be a sign that you need to break the sub routine into smaller units of work, which makes code reuse and debugging much easier.

选项 1 可能是最好的方法,因为它促进了更好的编码实践。如果您有很长的子例程,这可能表明您需要将子例程分解为更小的工作单元,这使得代码重用和调试更加容易。

Option 2 is appropriate in some cases where the condition truly is an error. One could argue that failing a validation test is a form of an error condition.

在条件确实是错误的某些情况下,选项 2 是合适的。有人可能会争辩说,验证测试失败是错误条件的一种形式。

Personally, I tend to avoid using Exit Sub and Exit Function (Option 3) in most cases because I like to have a single point in a sub routine where the routine exits and passes control back to the calling procedure. Having multiple exit points within a routine is a sign that you need to refactor your code \ rethink the logic you are applying. Sometimes it can't be helped, but often you create a situation that is more difficult to debug.

就我个人而言,在大多数情况下,我倾向于避免使用 Exit Sub 和 Exit Function(选项 3),因为我喜欢在子例程中有一个点,子例程在该点处退出并将控制权传递回调用过程。在一个例程中有多个退出点表明你需要重构你的代码\重新考虑你正在应用的逻辑。有时它无济于事,但通常您会创建更难以调试的情况。

回答by Aharpe

When you pop up the message box you should be able to use Exit Sub or Exit Function depending on what you procedure is. It should stop the execution of the code and exit the procedure.

当您弹出消息框时,您应该能够根据您的程序使用 Exit Sub 或 Exit Function。它应该停止代码的执行并退出程序。

回答by Lionel T.

I know it's late but i just wanted to add that if you want to stop running any VBA code, you can just use Endin your error handler for example. This way it won't return to the sub/function who called (if there is one).

我知道现在已经晚了,但我只想补充一点,如果你想停止运行任何 VBA 代码,你可以End在你的错误处理程序中使用。这样它就不会返回到调用的子/函数(如果有的话)。

Hope it helps.

希望能帮助到你。