vba 文本框空问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5662751/
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
Textbox null problem
提问by darkjh
I have a textbox and a button on my Access form. In the click event of the button i want to see if the textbox is empty, if it is, nothing will be executed. So i use
我的 Access 表单上有一个文本框和一个按钮。在按钮的单击事件中,我想查看文本框是否为空,如果为空,则不会执行任何操作。所以我用
If Me.textbox.Value = Null Then
Exit Sub
End if
But it doesn't work... I checked the textbox.value in the execution window and it is Null, but the if clause just doesn't work... Why?
但它不起作用......我检查了执行窗口中的textbox.value,它是Null,但if子句不起作用......为什么?
EDIT: @Dimse, I tried "", doesn't work. And also textbox.text = Null, it pops an error telling me the textbox is not active.. Very strange.
编辑:@Dimse,我试过“”,不起作用。还有 textbox.text = Null,它会弹出一个错误,告诉我文本框未处于活动状态.. 很奇怪。
回答by HansUp
Null is never equal to anything, not even Null. Use the IsNull()
function.
Null 永远不等于任何东西,甚至 Null 也不等于。使用该IsNull()
功能。
If IsNull(Me.textbox.Value) Then
If you want Me.textbox
treated the same when it contains an empty string as when it's Null, concatenate an empty string to it and check the length of the combined string:
如果您希望Me.textbox
在包含空字符串时与空字符串相同对待,请将空字符串连接到它并检查组合字符串的长度:
If Len(Me.textbox.Value & "") = 0 Then
You could also use the named constant, vbNullString
, instead of the string literal, ""
, for an empty string.
对于空字符串,您还可以使用命名常量 ,vbNullString
代替字符串文字 , ""
。
If Len(Me.textbox.Value & vbNullString) = 0 Then
Using the string literal requires VBA to construct that string from scratch each time. With the named constant, VBA only needs to reference it, so should be faster and use less memory. However in many (probably most) cases, the performance advantage with vbNullString
would be so minor that you wouldn't notice the difference. Also see the comment belowfrom David-W-Fenton.
使用字符串文字需要 VBA 每次从头开始构造该字符串。使用命名常量,VBA 只需要引用它,所以应该更快并且使用更少的内存。但是,在许多(可能是大多数)情况下, 的性能优势vbNullString
很小,您不会注意到差异。另请参阅以下来自David-W-Fenton的评论。
For me, the more compelling reason to use vbNullString
is that it's instantly recognizable to my aging eyes. Conversely, with the string literal, it takes (a tiny bit) longer for me to confirm that ""
is not actually something else ... like " "
or "'"
. The only downside with vbNullString
, IMO, is that requires more typing than ""
.
对我来说,使用vbNullString
它的更令人信服的理由是它可以立即被我老化的眼睛识别出来。相反,对于字符串文字,我需要(一点点)更长的时间来确认它""
实际上不是其他东西......比如" "
or "'"
。vbNullString
IMO的唯一缺点是需要比""
.
And finally, although you don't actually need to explicitly reference the Value
property (since it's the default property of a text box), I left it in because you had it that way and because I prefer to be explicit with Value
, too. :-)
最后,虽然您实际上并不需要显式引用该Value
属性(因为它是文本框的默认属性),但我还是将其保留了下来,因为您已经这样做了,而且我也更喜欢用 明确表示Value
。:-)
回答by rfb
I also apologize for being awaking the dead, but i'm wondering that no one has considered the use of Nz
Function (see it @MSDN), very popular in VBA, also usable in Access/SQL and in my opinion the more convenient, concise and powerful solution for nullable values in expressions.
我也为唤醒死者而道歉,但我想知道没有人考虑过使用Nz
Function(参见@MSDN),它在 VBA 中非常流行,也可用于 Access/SQL,在我看来更方便、简洁以及表达式中可为空值的强大解决方案。
回答by gregg
I apologize if I am awaking the dead, but just for completeness sake I am going to give the code for how to test for spaces (visibly 'blank/empty') as well:
如果我唤醒死者,我深表歉意,但为了完整起见,我还将提供有关如何测试空格(明显为“空白/空”)的代码:
If IsNull(Me.Textbox) Or Trim(Me.Textbox) = vbNullString Then
If Trim(Me.Textbox & vbNullString) = vbNullString Then 'Shorter version
If Len(Trim(Me.Textbox) & vbNullString) = 0 Then 'Shortest version
I came here looking for how to handle spaces, empty/ZLS, & NULL's
我来这里是为了寻找如何处理空格、空/ZLS 和 NULL 的
回答by Mark Mooibroek
Expand your sub like so:
像这样扩展你的子:
If is null(Me.textbox.Value) Or (Me.textbox.Value = "") Then
Exit Sub
End if
回答by Scorpio
Null is not equal to another Null ;)
Null 不等于另一个 Null ;)
try If isNull(Me.textbox.Value) Then
尝试 If isNull(Me.textbox.Value) Then
回答by Dimse
I think you may need to check againt "", the empty string, and not Null.
我认为您可能需要再次检查“”,空字符串,而不是 Null。
回答by Rambler
Just use a second criteria, that will work !!
In this case just a simple word like "check".
只需使用第二个标准,那就行了!!
在这种情况下,只是一个简单的词,如“检查”。
If Forms![Basic]![Table.Item] & "check" = "check" Then
MsgBox "Field Empty"
Else
MsgBox "Field Not Empty"
End If
回答by twoLeftFeet
I couldn't get this to work, since I was using the KeyUP event. So instead, this is what worked for me.
我无法让它工作,因为我使用的是 KeyUP 事件。所以相反,这对我有用。
If(Textbox.Text = '')
...
Since Textbox.Value only updates on change event, it wasn't updating on keyup, so Textbox.Text is what is currently in the box.
由于 Textbox.Value 仅在更改事件时更新,它不会在 keyup 上更新,因此 Textbox.Text 是当前在框中的内容。
Summary:Alternatively, Use the .Text property
摘要:或者,使用 .Text 属性