VBA 中使用 null 检查长度

发布于 2024-10-11 01:16:09 字数 540 浏览 0 评论 0原文

我有一些代码旨在检查文本框中值的长度,如果任何框中没有内容,则字符串的长度为 0 (或 null)。代码如下:

If (Len(Form_MainScreen.Ctl48.Value) Or Len(Form_MainScreen.Ctl49.Value) Or _
    Len(Form_MainScreen.Ctl50.Value) Or Len(Form_MainScreen.Ctl51.Value) Or _
    Len(Form_MainScreen.Ctl52.Value) Or Len(Form_MainScreen.Ctl53.Value) Or _
    Len(Form_MainScreen.Ctl54.Value) = 0) Then
              Do X
Else
              Do Y
End If

当一个字符串为空时,长度检查变为“null”,整个语句也变为“null”。 但如果长度检查全部不为空,则 if 语句变为“1”,然后再次执行 Do X 过程。

知道我能做什么吗?

谢谢, 蒂姆

I have some code that is meant to check the length of the values in the text boxes, and if any of the boxes has no content the length of the string is 0 (or null). Here is the code:

If (Len(Form_MainScreen.Ctl48.Value) Or Len(Form_MainScreen.Ctl49.Value) Or _
    Len(Form_MainScreen.Ctl50.Value) Or Len(Form_MainScreen.Ctl51.Value) Or _
    Len(Form_MainScreen.Ctl52.Value) Or Len(Form_MainScreen.Ctl53.Value) Or _
    Len(Form_MainScreen.Ctl54.Value) = 0) Then
              Do X
Else
              Do Y
End If

When one string is blank, the length check becomes "null" and so does the whole statement.
But if the length checks are all not null, the if statement becomes a "1" and then procedes to execute the Do X procedure again.

Any idea of what I can do?

Thanks,
Tim

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

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

发布评论

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

评论(2

清风无影 2024-10-18 01:16:09

您的代码片段对我来说没有多大意义,但如果您使用 vb.net 并且您希望“X”在任何值为 null 或 0 长度时执行,那么您可以执行以下操作:

If (String.IsNullOrEmpty(Form_MainScreen.Ctl48.Value) OrElse String.IsNullOrEmpty() OrElse _
    String.IsNullOrEmpty(Form_MainScreen.Ctl50.Value) OrElse String.IsNullOrEmpty(Form_MainScreen.Ctl51.Value) OrElse _
    String.IsNullOrEmpty(Form_MainScreen.Ctl52.Value) OrElse String.IsNullOrEmpty(Form_MainScreen.Ctl53.Value) OrElse _
    String.IsNullOrEmpty(Form_MainScreen.Ctl54.Value)) Then
    X()
Else
    Y()
End If 

使用 String.IsNullOrEmpty 是一个内置函数,它将无一例外地处理这两种情况。使用 OrElse 条件将允许稍微更快的处理速度,因为一旦收到有效的匹配,它将停止执行条件表达式。简单地使用 Or 需要评估所有条件表达式,即使这不是必需的。

Your code snippet doesn't make much sense to me, but if you're using vb.net and you want "X" to execute if ANY of the values is null or 0 length then you could do the following:

If (String.IsNullOrEmpty(Form_MainScreen.Ctl48.Value) OrElse String.IsNullOrEmpty() OrElse _
    String.IsNullOrEmpty(Form_MainScreen.Ctl50.Value) OrElse String.IsNullOrEmpty(Form_MainScreen.Ctl51.Value) OrElse _
    String.IsNullOrEmpty(Form_MainScreen.Ctl52.Value) OrElse String.IsNullOrEmpty(Form_MainScreen.Ctl53.Value) OrElse _
    String.IsNullOrEmpty(Form_MainScreen.Ctl54.Value)) Then
    X()
Else
    Y()
End If 

Using String.IsNullOrEmpty is a built-in function that will handle both conditions without exception. Using the OrElse conditional will allow slightly faster processing in that it will stop executing the conditional expressions once it receives a valid match. Using simply Or requires all conditional expressions to be evaluated even if it's not necessary.

明媚如初 2024-10-18 01:16:09
If (Len(Form_MainScreen.Ctl48.Value)=0 Or Len(Form_MainScreen.Ctl49.Value)=0 Or _
    Len(Form_MainScreen.Ctl50.Value)=0 Or Len(Form_MainScreen.Ctl51.Value)=0 Or _
    Len(Form_MainScreen.Ctl52.Value)=0 Or Len(Form_MainScreen.Ctl53.Value)=0 Or _
    Len(Form_MainScreen.Ctl54.Value) = 0) Then
    Do X
Else
    Do Y
End If
If (Len(Form_MainScreen.Ctl48.Value)=0 Or Len(Form_MainScreen.Ctl49.Value)=0 Or _
    Len(Form_MainScreen.Ctl50.Value)=0 Or Len(Form_MainScreen.Ctl51.Value)=0 Or _
    Len(Form_MainScreen.Ctl52.Value)=0 Or Len(Form_MainScreen.Ctl53.Value)=0 Or _
    Len(Form_MainScreen.Ctl54.Value) = 0) Then
    Do X
Else
    Do Y
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文