文本框为空问题

发布于 2024-11-01 02:48:30 字数 296 浏览 1 评论 0原文

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

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

但它不起作用...我检查了执行窗口中的textbox.value,它是Null,但是if子句不起作用...为什么?

编辑:@Dimse,我尝试了“”,不起作用。而且 textbox.text = Null,它会弹出一个错误,告诉我文本框未激活。非常奇怪。

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

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

征棹 2024-11-08 02:48:30

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

If IsNull(Me.textbox.Value) Then

如果您希望 Me.textbox 包含空字符串时的处理方式与 Null 时的处理方式相同,请将空字符串与其连接并检查组合字符串的长度:

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

您还可以使用命名常量, vbNullString,而不是空字符串的字符串文字 ""

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

使用字符串文字需要 VBA 每次从头开始构造该字符串。使用命名常量,VBA 只需要引用它,因此应该更快并且使用更少的内存。然而,在许多(可能是大多数)情况下,vbNullString 的性能优势非常小,以至于您不会注意到其中的差异。另请参阅下面的评论来自大卫-W-芬顿

对我来说,使用 vbNullString 更令人信服的原因是我衰老的眼睛可以立即识别它。相反,对于字符串文字,我需要(一点点)更长的时间来确认 "" 实际上不是其他东西......比如 " "“'”。在我看来,vbNullString 的唯一缺点是它比 "" 需要更多的输入。

最后,虽然您实际上不需要显式引用 Value 属性(因为它是文本框的默认属性),但我将其保留在其中,因为您已经这样做了,而且我更喜欢也应明确使用 Value。 :-)

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

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:

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

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

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.

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

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

空袭的梦i 2024-11-08 02:48:30

我也为唤醒死者而道歉,但我想知道没有人考虑过使用 Nz 函数(请参阅@MSDN),在 VBA 中非常流行,也可在 Access/SQL 中使用,在我看来更有用针对表达式中可为空值的方便、简洁且强大的解决方案。

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.

偏闹i 2024-11-08 02:48:30

如果我唤醒死者,我深表歉意,但为了完整起见,我将提供 如何测试空格(明显为“空白/空”)

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

我来这里是为了寻找如何处理空格、空/ZLS、& NULL 的

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

鸢与 2024-11-08 02:48:30

Null 不等于另一个 Null ;)

尝试 If isNull(Me.textbox.Value) then

Null is not equal to another Null ;)

try If isNull(Me.textbox.Value) Then

时光瘦了 2024-11-08 02:48:30

像这样扩展你的子目录:

If is null(Me.textbox.Value) Or (Me.textbox.Value = "") Then
    Exit Sub
End if

Expand your sub like so:

If is null(Me.textbox.Value) Or (Me.textbox.Value = "") Then
    Exit Sub
End if
无可置疑 2024-11-08 02:48:30

我认为您可能需要再次检查“”、空字符串,而不是 Null。

I think you may need to check againt "", the empty string, and not Null.

夜司空 2024-11-08 02:48:30

我无法让它工作,因为我正在使用 KeyUP 事件。所以相反,这对我有用。

If(Textbox.Text = '') 
  ...

由于 Textbox.Value 仅在更改事件时更新,因此它不会在 keyup 时更新,因此 Textbox.Text 是当前框中的内容。

摘要: 或者,使用 .Text 属性

I couldn't get this to work, since I was using the KeyUP event. So instead, this is what worked for me.

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.

Summary: Alternatively, Use the .Text property

只需使用第二个标准,就可以了!!
在这种情况下,只需一个简单的词,如“检查”。

If Forms![Basic]![Table.Item] & "check" = "check" Then

MsgBox "Field Empty"

Else

MsgBox "Field Not Empty"

End If

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
¢蛋碎的人ぎ生 2024-11-08 02:48:30

我在某处看到了这个,想分享一下:

If Len(Trim(Me.txtBox.Value)) > 0 然后

I saw this somewhere and thought I'd share:

If Len(Trim(Me.txtBox.Value)) > 0 Then

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文