vba 检查访问表单字段是否为空,如果不为空则调用添加功能

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

Check Access Form Field for Null, if Not Null then Call the Add Feature

vbams-accessaccess-vba

提问by T-Rex

I have this simple Access Form that when you fill out the form and forget to fill out the Business Unit field, a msgBox will pop up telling you so and setFocus to that very Combo box. If it is Not null, I want to call the next feature. The first part of this code works, but the when it is notNull it wil not carry on.

我有一个简单的访问表单,当您填写表单并忘记填写业务单位字段时,会弹出一个 msgBox 告诉您,并将焦点设置到那个组合框。如果它不为空,我想调用下一个功能。这段代码的第一部分有效,但当它不为空时它不会继续。

 Private Sub Image_AddNon_RPS_Button_Click()

 If IsNull(Me.BU_Selected_Add) Then
    MsgBox "Please Select a Business Unit!", vbOKOnly
            Exit Sub
         End If
    Me.Combo_BU_Selector.SetFocus
    Exit Sub
If Not IsNull(Me.BU_Selected_Add) Then
    Call Add_RPS_LINE
        End If
End Sub

Does anybody see where I am totally out in left field?

有没有人看到我在左场完全出局?

回答by Ken White

You've got an extra Exit Sub(the one after the first MsgBox) that stops your code from doing what you want. Also, your first End Ifis in the wrong location.

你有一个额外的Exit Sub(第一个之后的MsgBox)阻止你的代码做你想做的事。此外,您的第一个End If位置错误。

Try something like this instead:

试试这样的:

Private Sub Image_AddNon_RPS_Button_Click()

  If IsNull(Me.BU_Selected_Add) Then                     ' No business unit 
      MsgBox "Please Select a Business Unit!", vbOKOnly  ' Tell user 
      Me.Combo_BU_Selector.SetFocus                      ' Focus the control
      Exit Sub                                           ' Exit the method
  End If                                                 ' End the IsNull test

  Call Add_RPS_LINE      ' You only get here if the above doesn't execute
End Sub

It helps if you learn to properly indent your code to match Ifand End Ifvisually, so you can see where they line up (match) and where they don't. :-)

如果您学会正确缩进代码以匹配IfEnd If视觉化,这会有所帮助,这样您就可以看到它们排列(匹配)的位置以及它们不排列的位置。:-)

回答by unDeadHerbs

If you correct the indentation of your code to:

如果您将代码的缩进更正为:

Private Sub Image_AddNon_RPS_Button_Click()
   If IsNull(Me.BU_Selected_Add) Then
      MsgBox "Please Select a Business Unit!", vbOKOnly
      Exit Sub
   End If
   Me.Combo_BU_Selector.SetFocus
   Exit Sub
   If Not IsNull(Me.BU_Selected_Add) Then
      Call Add_RPS_LINE
   End If
End Sub

You can clearly see that the Exit Subin the middle will terminate the function before it gets to Call Add_RPS_LINE.

您可以清楚地看到Exit Sub中间的 将在到达 之前终止函数Call Add_RPS_LINE

If you look at the two Ifstatements you have, you can see that they are almost the same and so an Elseis in order, resulting in this simpler and more readable code.

如果您查看If您拥有的两个语句,您会发现它们几乎相同,因此Else顺序一致,从而产生了更简单、更易读的代码。

Private Sub Image_AddNon_RPS_Button_Click()
   If IsNull(Me.BU_Selected_Add) Then
      MsgBox "Please Select a Business Unit!", vbOKOnly
      Me.Combo_BU_Selector.SetFocus
   Else
      Call Add_RPS_LINE
   End If
End Sub