Reputation: 3
I have bunch of input TextBoxes. I need to check all TextBoxes are filled before processing. Since I have 17 TextBoxes, can any one help me to write a code to check all.
I have tried by using If ... Then ... Else
but it seems not working in my case.
Upvotes: 0
Views: 3363
Reputation: 57743
You can …
… loop through all controls
For Each Ctrl In Me.Controls
… then check if the current control is a TextBox
If TypeName(Ctrl) = "TextBox" Then
… and then check if it is empty
If Ctrl.Text = vbNullString Then
So you end up with something like this:
Option Explicit
Public Sub ValidateCheckBoxes()
Dim EmptyBoxesFound As Boolean
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "TextBox" Then
If Ctrl.Text = vbNullString Then
EmptyBoxesFound = True
Exit For
End If
End If
Next Ctrl
If EmptyBoxesFound Then
MsgBox "At least one box is not filled.", vbExclamation
Else
MsgBox "All boxes are filled.", vbInformation
End If
End Sub
Upvotes: 4