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

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

问题描述

我有一个文本框,并在我的访问形式的按钮。在我想看看如果文本框为空,按钮的单击事件,如果是,什么也不会被执行。 所以我用

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,它是空的,但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.

推荐答案

空绝不等于任何东西,甚至不能为空。使用ISNULL()函数。

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

If IsNull(Me.textbox.Value) Then

如果你想Me.textbox同等对待时,它包含一个空字符串时,它的空,串联一个空字符串,并检查组合字符串的长度:

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 的性能优势会如此轻微的,你不会注意到其中的差别。另请参阅下面的评论从大卫芬顿。

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 (maybe 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 Fenton.

对我来说,更令人信服的理由使用 vbNullString 的是,它立即识别到我的眼睛老化。相反,用字符串,它需要(一点点),再由我来确认其实不是别的东西......就像 。与 vbNullString 唯一的缺点,国际海事组织,就是需要比输入更多的文字

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属性,因为它是一个文本框的默认属性。我离开它,因为你有这样的说法。

And finally, I don't think you should actually need to explicitly reference the Value property because it's the default property of a text box. I left it in because you had it that way.

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

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