View Single Post
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default If Then's for 12 textboxes to check if they are empty.

If you have 14 textboxes and you only have to validate 12 of them, then I'd
either use nice names and loop through the nice names (tb01 through tb12).

But if you want to check all the textboxes (no matter how many), you could put
your validation into the "ok" button. (I added a label to show any error.)

Option Explicit
Private Sub CommandButton1_Click()
Dim ctrl As Control
Dim ErrorFound As Boolean

ErrorFound = False
Me.Label1.Caption = ""
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then
If ctrl.Object.Value = "" Then
ErrorFound = True
ctrl.SetFocus
Me.Label1.Caption = "Please fix textboxes!"
Exit For
End If
End If
Next ctrl

If ErrorFound = True Then
Exit Sub
End If

'rest of code

End Sub



Beertje wrote:

I have 12 textboxes for data on a userform. Before writing the data to the
worksheet I need to check whether the text boxes are empty or not. If empty
then a message pops up to enter data into the textbox with focus.
Do I need to write 12 separate if thens? Or can I apply an easier quicker
method?


--

Dave Peterson