PDA

View Full Version : Validating more than one textbox for Null values



RTR97
08-07-2012, 12:51 PM
Hello All,

I have working code to check a textbox for null values, etc. however I am not certain how to implement this code for let's say 5 textboxes. Any advice will be greatly appreciated.

Private Sub CommandButton1_Click()

If Trim(textBox1 & "") = vbNullString Then
MsgBox "Please Enter a numerical value in textBox1"
Else
Range("myCell") = ">" & Me.textBox1.Value
End If
End Sub

Bob Phillips
08-07-2012, 02:56 PM
Private Sub CommandButton1_Click()
Dim msg As String

If Trim(TextBox1.Text) = vbNullString Then
msg = "TextBox1"
Else
Range("myCell") = ">" & Me.TextBox1.Value
End If

If Trim(TextBox2.Text) = vbNullString Then
msg = "TextBox2"
Else
Range("myCell") = ">" & Me.TextBox2.Value
End If

If Trim(TextBox3.Text) = vbNullString Then
msg = "TextBox3"
Else
Range("myCell") = ">" & Me.TextBox3.Value
End If

If Trim(TextBox4.Text) = vbNullString Then
msg = "TextBox4"
Else
Range("myCell") = ">" & Me.TextBox4.Value
End If

If Trim(TextBox5.Text) = vbNullString Then
msg = "TextBox5"
Else
Range("myCell") = ">" & Me.TextBox5.Value
End If

If msg <> "" Then

MsgBox "Please enter a numerical value in the following: " & vbNewLine & msg, vbInformation, "Incomplete data"
End If
End Sub