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
Solved

Text box validation

Posted on 2014-03-21
11
256 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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …

837 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