文本框空问题 [英] Textbox null problem

查看:28
本文介绍了文本框空问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Access 表单上有一个文本框和一个按钮.在按钮的单击事件中,我想查看文本框是否为空,如果为空,则不会执行任何操作.所以我用

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

If Me.textbox.Value = Null Then
    Exit Sub
End if

但它不起作用...我检查了执行窗口中的 textbox.value 并且它是 Null,但是 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?

@Dimse,我试过",不起作用.还有 textbox.text = Null,它会弹出一个错误,告诉我文本框未处于活动状态.. 很奇怪.

@Dimse, I tried "", doesn't work. And also textbox.text = Null, it pops an error telling me the textbox is not active.. Very strange.

推荐答案

Null 永远不等于任何东西,甚至 Null.使用 IsNull() 函数.

Null is never equal to anything, not even Null. Use the IsNull() function.

If IsNull(Me.textbox.Value) Then

如果您希望 Me.textbox 在包含空字符串时与为 Null 时一样对待,请连接一个空字符串并检查组合字符串的长度:

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:

If Len(Me.textbox.Value & "") = 0 Then

对于空字符串,您还可以使用命名常量 vbNullString 代替字符串文字 "".

You could also use the named constant, vbNullString, instead of the string literal, "", for an empty string.

If Len(Me.textbox.Value & vbNullString) = 0 Then

使用字符串字面量需要 VBA 每次都从头开始构造该字符串.使用命名常量,VBA 只需要引用它,所以应该更快并且使用更少的内存.然而,在许多(可能是大多数)情况下,vbNullString 的性能优势很小,您不会注意到差异.另请参阅来自 下面的评论/users/9787/david-w-fenton">大卫-W-芬顿.

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 below from David-W-Fenton.

对我来说,使用 vbNullString 的更令人信服的理由是它可以立即被我老化的眼睛识别出来.相反,对于字符串文字,我需要(一点点)更长的时间来确认 "" 实际上不是别的东西......比如 " ">"'".vbNullString,IMO,唯一的缺点是需要比 "" 更多的输入.

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 "".

最后,虽然您实际上并不需要显式引用 Value 属性(因为它是文本框的默认属性),但我还是将其保留了下来,因为您已经这样做了,因为我也更喜欢用 Value 来明确表示.:-)

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. :-)

这篇关于文本框空问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆