Solved

Text box validation

Posted on 2014-03-21
11
244 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
Comment Utility
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
Comment Utility
What's StringBuilder ?
0
 
LVL 9

Expert Comment

by:dustock
Comment Utility
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
 
LVL 7

Expert Comment

by:COACHMAN99
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
@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
Comment Utility
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
Comment Utility
Sorry - didn't see that :-(
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now