vba if 语句中的组合框为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19929186/
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
Combobox null in if statement
提问by D347HxD
I am trying to code an if statement where if a certain combobox is null, then it runs a certain part of code if it has data in it then it runs another. I wrote up this:
我正在尝试编写一个 if 语句,如果某个组合框为空,那么它运行代码的某个部分,如果它有数据,然后它运行另一个。我写了这个:
Private Sub ProjectAddSetDateAutoBtn_Click()
If ProjectAddAllDueDateAutoCmBx = Null Then
'Code1
Msgbox("ComboBox Is Null")
Else
'Code2
Msgbox("ComboBox Has Data")
End If
End Sub
I leave the combobox with no data, and then it doesn't run the code in the first part of the if or the code in the 2nd part of it either! If I enter data into the box, it runs the 2nd part of the if statement perfectly. There are no errors, I am quite stumped on this. Do ComboBoxes have their own "Null"? Is there a problem with this if statement?
我让组合框没有数据,然后它不会运行 if 的第一部分中的代码或它的第二部分中的代码!如果我在框中输入数据,它会完美地运行 if 语句的第二部分。没有错误,我对此非常困惑。ComboBoxes 有自己的“Null”吗?这个 if 语句有问题吗?
回答by HansUp
Nothing is ever equal to Null, not even another Null.
没有任何东西等于 Null ,甚至另一个 Null 也不行。
Use IsNull()to check whether the combo box is Null.
使用IsNull()检查组合框是否为 Null。
'If ProjectAddAllDueDateAutoCmBx = Null Then
If IsNull(ProjectAddAllDueDateAutoCmBx) = True Then
回答by pteranodon
You cannot use a = Null
comparison to get the results you want because Null propagates. To see this in action, try:
您不能使用= Null
比较来获得想要的结果,因为 Null 会传播。要查看此操作,请尝试:
? Null = Null
in the Immediate Window and you'll see that Null is returned. Use the IsNull function, which will return true or false as you would expect.
在立即窗口中,您将看到返回 Null。使用 IsNull 函数,它会按照您的预期返回 true 或 false。
Private Sub ProjectAddSetDateAutoBtn_Click()
If IsNull(ProjectAddAllDueDateAutoCmBx) Then
'Code1
Msgbox("ComboBox Is Null")
Else
'Code2
Msgbox("ComboBox Has Data")
End If
End Sub
回答by Oliver
While the accepted answer is totally correct, I use a different approach:
虽然接受的答案是完全正确的,但我使用了不同的方法:
If HasValue(ProjectAddAllDueDateAutoCmBx) Then
where the HasValue function is:
其中 HasValue 函数是:
Public Function HasValue(v As Variant) As Boolean
If Trim(v & "") <> "" Then
HasValue = True
Else
HasValue = False
End If
End Function
This has the advantage of treating NULL and "" (or any pure whitespace) values the same, which is many times what you want with MSAccess controls. For example entering a value in a null-valued textbox and removing it again with backspace will result in a ""-value, not NULL. From a user-perspective this is mostly meant to be the same.
这具有将 NULL 和 ""(或任何纯空白)值处理为相同的优点,这对于 MSAccess 控件来说是您想要的很多倍。例如,在空值文本框中输入一个值并用退格键再次将其删除将导致 ""-value,而不是 NULL。从用户的角度来看,这主要是相同的。
[The (v & "")-part is just a trick to force conversion to a string.]
[ (v & "") 部分只是强制转换为字符串的技巧。]
回答by Heinzi
I would suggest
我会建议
If IsNull(ProjectAddAllDueDateAutoCmBx.Value) Then
It correctly checks for Null
(IsNull
instead of = Null
), and it explicitly checks the valueof the combo box.
它正确检查Null
(IsNull
而不是= Null
),并明确检查组合框的值。
(In most cases -- depending on the context -- just using the name of the control yields the value, but it doesn't hurt to be explicit.)
(在大多数情况下 - 取决于上下文 - 仅使用控件的名称会产生值,但明确表示并没有什么坏处。)
回答by ZaoTaoBao
the equivalent of null in VB is Nothing so your check wants to be:
VB 中 null 的等价物是 Nothing 所以你的支票是:
If ProjectAddAllDueDateAutoCmBx Is Nothing Then
....
....
it hope helps.
它希望有所帮助。