首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IsNull ComboBox

IsNull ComboBox
EN

Stack Overflow用户
提问于 2018-09-11 13:36:36
回答 2查看 60关注 0票数 0

我正在试着检查用户是否已经完成了我表单中的所有字段。以下代码适用于所有字段,但我的多选择组合框有问题。现在,如果我将comboBox留空,下面的代码将完美地工作,并将bg颜色发送到红色。但是,如果我选择的东西确实很好,那么在下面的line..If IsNull(ctrl)或Len(ctrl) =0处会出现一个不匹配错误。

代码语言:javascript
复制
Private Sub AddEmployee_Click()
    If CheckForEmpty = False Then
        MsgBox "Please fill in red boxes"
    Else

    End If
End Sub

Function CheckForEmpty() As Boolean
    CheckForEmpty = True
    ClearControlFormatting

    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.Tag = "FILL" Then
            If IsNull(ctrl) Or Len(ctrl) = 0 Then
                ctrl.BackColor = vbRed
                CheckForEmpty = False
            End If
        End If
    Next
End Function

Sub ClearControlFormatting()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.Tag = "FILL" Then
            ctrl.BackColor = vbWhite
        End If
    Next    
End Sub

Private Sub Form_Current()
    ClearControlFormatting    
End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-11 14:23:28

多值组合框的值是数组,因此类型不匹配.

使用IsArray测试是否设置了:

代码语言:javascript
复制
Function CheckForEmpty() As Boolean
    CheckForEmpty = True
    ClearControlFormatting

    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.Tag = "FILL" Then
            If Not IsArray(ctrl) Then
                If IsNull(ctrl) OR Len(ctrl.Value) = 0 Then
                    ctrl.BackColor = vbRed
                    CheckForEmpty = False         
                End If
            End If
        End If
    Next
End Function
票数 2
EN

Stack Overflow用户

发布于 2018-09-11 13:48:54

尝试以下几点:

代码语言:javascript
复制
Dim ctrl As Control
For Each ctrl In Me.Controls
    If ctrl.Tag = "FILL" Then
        If Len(ctrl.Value & "") = 0 Then
            ctrl.BackColor = vbRed
            CheckForEmpty = False
        End If
    End If
Next ctrl

当您将零长度字符串""连接到VBA中的值时,它将隐式地将该值转换为字符串,此时您可以安全地检查字符串的长度,以确定用户是否输入/选择了一个值。

这种类型的比较保存了验证中的一个步骤,并在尝试检查空值的Len()时正确处理。Len(Null) = Null不是零,因此当前的测试有可能出现运行时逻辑错误。

如果控件在屏幕上是“空”的,这取决于其绑定到字段的方式,它可以计算为零长度字符串或空值,从而在Len()测试中产生不同的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52277234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档