Solved

Text box validation

Posted on 2014-03-21
11
260 Views
Last Modified: 2014-04-06
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.
0
Comment
Question by:lincstech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 39946020
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
 

Author Comment

by:lincstech
ID: 39946048
What's StringBuilder ?
0
 
LVL 9

Expert Comment

by:dustock
ID: 39946113
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39946317
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
 

Author Comment

by:lincstech
ID: 39947297
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39948858
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
 
LVL 40
ID: 39949631
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
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39950712
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
 
LVL 40
ID: 39950728
@COACHMAN99

This is not an Access question, this is a .NET question. There is no BeforeUpdate event in .NET.
0
 
LVL 9

Accepted Solution

by:
dustock earned 500 total points
ID: 39950742
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
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39950745
Sorry - didn't see that :-(
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question