Text box validation

Hello,

Can someone advise me on a way to check multiple textboxes have text before preceding to the next step. I also need to display which textbox is empty.
lincstechAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
dustockConnect With a Mentor Commented:
If you use modify the block of code I provided already slightly you could do something like this...

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If validateText() Then
            'Insert data into datbase
        End If
    End Sub


    Private Function validateText() As Boolean
        Dim sb As StringBuilder
        Dim ctrl As Control

        sb = New StringBuilder()

        For Each ctrl In Me.Controls
            If (ctrl.GetType() Is GetType(TextBox)) Then
                If ctrl.Text.Length = 0 Then
                    sb.Append(ctrl.Name & " cannot be blank")
                    sb.AppendLine()
                End If
            End If
        Next

        If sb.ToString().Length > 0 Then
            lblError.Text = sb.ToString()
            Return False
        Else
            Return True
        End If
    End Function

Open in new window

0
 
dustockCommented:
This will go through all the controls on your form and if it encounters a textbox it will see if its blank.


    Private Sub validateText()
        Dim sb As StringBuilder
        Dim ctrl As Control

        sb = New StringBuilder()

        For Each ctrl In Me.Controls
            If (ctrl.GetType() Is GetType(TextBox)) Then
                If ctrl.Text.Length = 0 Then
                    sb.Append(ctrl.Name & " cannot be blank")
                    sb.AppendLine()
                End If
            End If
        Next

        lblError.Text = sb.ToString()
    End Sub

Open in new window

0
 
lincstechAuthor Commented:
What's StringBuilder ?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
dustockCommented:
I forgot to mention you need to add the imports statement for the StringBuilder,
Imports System.Text

Open in new window


As far as what it is, its a way to store strings of data.  I could create a string and keep appending data as:
Dim strText as String
strText =+ "information" & Environment.NewLine

Open in new window


But instead I use the StringBuilder class and the methods that are available.

When ever I use loops where information needs returned to the screen I use a StringBuilder to store all of my information and then display all the data after the loop has finished.

You can get more information from MSDN http://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx
and
http://msdn.microsoft.com/en-us/library/2839d5h5%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
0
 
COACHMAN99Commented:
a possible alternative is to add the validation code to before form update and form insert events

Private Sub Form_BeforeUpdate(Cancel As Integer)
  if NZ(text0,"") = "" then msgbox .. : Cancel=true
  if NZ(text1,"") = "" then msgbox .. : Cancel=true

this wont give a single message (as above) but one for each omission.
0
 
lincstechAuthor Commented:
destock:

Very good one more thing I need to put add a section which allows the code to move on to the next section if all textboxes are full in my case I need it to commit the saving of the data.
0
 
CodeCruiserCommented:
Here is one way of doing it

private sub btnSave_Click...
If String.IsNullOrEmpty(txtName.Text) Then
   msgbox "Name is required."
   txtName.Focus
   Exit Sub
End If
If string.IsNullOrEmpty(txtAddress.text) Then
   msgbox "Address is required."
   txtAddress.Focus
   Exit Sub
End If
...
'Rest of the save code which will execute if all required textboxes have been populated
End Sub
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
There is an ErrorProvider control in the Framework that enables you to easily create a blinking icon with a tooltip that you provide beside a given control to indicate that it contains a bad value.

Simply drop one on the Form, and use the following:

		For Each ctl As Control In Me.Controls
			If TypeOf ctl Is TextBox Then
				If ctl.Text.Length = 0 Then
					ErrorProvider1.SetError(ctl, "This field cannot be empty")
				Else
					ErrorProvider1.SetError(ctl, "")
				End If
			End If
		Next

Open in new window

0
 
COACHMAN99Commented:
If you set Cancel = true the process will continue to the end of the sub, and save the data.

Private Sub Form_BeforeUpdate(Cancel As Integer)
  if NZ(text0,"") = "" then msgbox .. : Cancel=true
  if NZ(text1,"") = "" then msgbox .. : Cancel=true
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
@COACHMAN99

This is not an Access question, this is a .NET question. There is no BeforeUpdate event in .NET.
0
 
COACHMAN99Commented:
Sorry - didn't see that :-(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.