PDA

View Full Version : Textbox Validation



av8tordude
11-08-2012, 10:11 AM
I have 13 textboxes in a userform. I want to check if at least 1 textbox has a value. If at least 1 textbox has a value, then do nothing. If neither textboxes has a value then display a msgbox only once. I'm using this code below, but it displays the msgbox for each textbox that is empty and displays the msgbox even if all the textboxes are filled...



Private Sub Enter_Click()
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If InStr(1, Ctrl.Name, "TextBox") > 0 Then

MsgBox "No Value entered" ' Display the text

End If

Next
End Sub

Kenneth Hobs
11-08-2012, 10:21 AM
Use End For after MsgBox.

av8tordude
11-08-2012, 10:25 AM
Ok that help, but now it displays the msgbox even if all the textboxes are filled...

Kenneth Hobs
11-08-2012, 01:37 PM
Private Sub CommandButton1_Click()
TextBoxesBlank
Unload Me
End Sub

Private Sub TextBoxesBlank()
Dim Ctrl As msforms.Control, tf As Boolean
tf = True
For Each Ctrl In Me.Controls
If VarType(Ctrl) = 8 And Ctrl.Value <> "" Then
tf = False
Exit For
End If
Next
If tf Then MsgBox "All textbox values are empty."
End Sub