2016-04-24 73 views
0

我有一个用户窗体包含多个组合框和文本框,用于向工作表中输入数据。我一直在寻找的是在将数据保存到工作表前检查所有这些框的代码,因此如果其中一个为空,它将弹出一条消息并且不保存数据。检查多个文本框/组合框的输入

我一直在使用一个函数来逐个检查每一个函数,但如果其他函数是空白的,它仍然会将数据保存到工作表中。

Public Function CheckEmpty(text_box As Object) As Boolean 
CheckEmpty = (Len(Trim(text_box.Value)) > 0) 
End Function 

回答

0

创建一个复选框控件对象的数组,然后遍历它,依次检查每个对象。

+0

这是什么样的我放在一起,它总是告诉我,这是不是空的,我新的所有这一切如此容忍我:昏暗测试(1至10)作为变 集试验(1)= TextBox1中 集试验(2)= TextBox2中 集试验(3)= TextBox3 集试验(4)= ComboBox1 Set Test(5)= ComboBox2 Set Test(6)= ComboBox3 如果为IsEmpty(测试)然后 MSGBOX“其空” 否则 MSGBOX“它不是” 结束如果 – Higgs

1

你可能想尝试类似如下

Private Sub CommandButton1_Click() '<== change "CommandButton1" with the actual 'closing' button name 
Dim ctrl As Control 
Dim msg As String 

With Me 
    For Each ctrl In .Controls 

     Select Case TypeName(ctrl) 

      Case "ComboBox" 
       If ctrl.ListIndex = -1 Then msg = msg & vbCrLf & "ComboBox '" & ctrl.name & "' with no value selected" 
      Case "TextBox" 
       If ctrl.text = "" Then msg = msg & vbCrLf & "TextBox '" & ctrl.name & "' with no value selected" 
      Case Else 

     End Select 

    Next ctrl 

    If msg = "" Then 
     .Hide ' hide the userform only if no empty textboxes and/or comboboxes 
    Else 
     MsgBox msg, vbExclamation + vbInformation 
    End If 
End With 

End Sub 

被放置在用户窗体代码窗格

+0

这创造了奇迹!感谢您的帮助。 – Higgs

+0

很高兴能有所帮助。如果我已经完成了您的问题,请将我的答案标记为已接受。谢谢 – user3598756