11 Replies - 10779 Views - Last Post: 30 November 2010 - 04:24 AM Rate Topic: -----

#1 Rohella  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 05-November 10

Loop through text boxes to check if they are empty before saving recor

Posted 16 November 2010 - 03:29 AM

Hi guys,
Its me again, sorry but im just stuck on this last thing. I have an insert statement which works fine but before the record is saved I want to validate the text boxes. So I just need to know how to check for empty text boxes before the save button is clicked on. I have seen a couple of examples like:

Dim ctr As TextBox

If ctr.Text = vbNullString Then
MsgBox("empty box")
End If

OR the use of String.empty etc, I just need to know how to include this in a loop before the following statement:

Dim con As SqlConnection
Dim cmd As SqlCommand
con = New SqlConnection(My.Settings.ConnectionString)
con.Open()
cmd = New SqlCommand("insert into CommissionStructure values(" + txtAgentNo.Text + ",'"...etc)", con)
cmd.ExecuteNonQuery()
con.Close()

Thank you :)

Is This A Good Question/Topic? 0
  • +

Replies To: Loop through text boxes to check if they are empty before saving recor

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Loop through text boxes to check if they are empty before saving recor

Posted 16 November 2010 - 04:41 AM

Hi,

You use legacy vb code... the new way to check is like this

If Textbox1.Text Is DBNull.Value OrElse TextBox1.Text.Trim() = "" Then
   MessageBox.Show("Please enter a value into Textbox1")
   'leave the sub and do not update anything
   Exit Sub
End If



This however does not prevent SQL injection (as i see how you want to do your update later on)

So you should investigate into Parameters for your queries/statements and how to formulate the sql statements correctly...
Was This Post Helpful? 1
  • +
  • -

#3 Rohella  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 05-November 10

Re: Loop through text boxes to check if they are empty before saving recor

Posted 16 November 2010 - 04:57 AM

I have this loop but it doesnt really do anything, it just loops through all the text boxes and then displays the message, I think it just needs tweeking to make it work the way I want it to but I cant put my finger on it.

Thank you so much for your reply :)

oops forgot to add the code, my bad sorry lol

For Each c As Control In Me.Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Text = String.Empty
End If
Next
MsgBox("not empty box")
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Loop through text boxes to check if they are empty before saving recor

Posted 16 November 2010 - 05:16 AM

post the code you've got so far, hard to tell you what's going wrong with it when we don't know what it's doing

Edit: nevermind, you added it already while I was fooling around lol

This post has been edited by Nakor: 16 November 2010 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Loop through text boxes to check if they are empty before saving recor

Posted 16 November 2010 - 06:48 AM

Rohella, please wrap your code between code tags (Hilight the code and click on the <> button on the toolbar of the topic window)


Is this what you are wanting to do?

For Each c As Control In Me.Controls
  If TypeOf c Is TextBox Then
    If CType(c, TextBox).Text = String.Empty Then
      messagebox.show("Empty Textbox Found")
      ctype(c, textbox).focus
      exit for
    end if
  End If
Next

Was This Post Helpful? 0
  • +
  • -

#6 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Loop through text boxes to check if they are empty before saving recor

Posted 17 November 2010 - 03:29 AM

And then you place the Textboxes into a TabControl or Panel... tabcontrol even worse, what message are you going to say? "Empty Textbox Found", set the cursor onto that Textbox, wich might sit on a tab page that is currently not visible and let the user guess where it is...

Now because the Textbox would be in a Tabcontrol or Panel. It's no longer found if "Me.Controls", so you will most probably need a recursive function to check every control in control in control etc...

Why not use the ErrorProvider control?
Was This Post Helpful? 0
  • +
  • -

#7 Rohella  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 05-November 10

Re: Loop through text boxes to check if they are empty before saving recor

Posted 17 November 2010 - 04:55 AM

@ motcom: Good tip but unfortunatly I cant use the ErrorProvider control because i am doing and asp.net web application in vb and that option doesnt apear :( ...

@CharlieMay: The code is still doing the same thing, it loops through all text boxes but doesnt display the message each time it finds an empty text box. It just loops through the controls and ends. I added a message at the end of "Next" to see if it doesnt display the message if their is an empty text box, but the message still displayed. Im stuck :(

Thank you so much for the reply guys :)
Was This Post Helpful? 0
  • +
  • -

#8 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Loop through text boxes to check if they are empty before saving recor

Posted 17 November 2010 - 01:46 PM

I would use the "String.IsNullOrEmpty" Command like this:

For Each c As Control In Me.Controls
 If TypeOf c Is TextBox Then
   If String.IsNullOrEmpty(CType(c, TextBox).Text) Then
     MessageBox.Show("Empty Textbox Found")
     CType(c, TextBox).Focus()
     Exit For
   End If
 End If
Next



On another thought, I had to do something like this recently and had to loop through all of the child controls to check them to. I just passed in the form to a sub and let it do the work. The code isn't exactly the same but you could adapt it.

Private Sub SetText(ByVal inctl As Control, ByVal inValue As Integer, ByVal inItemToFind As String)
		For Each ctl As Control In inctl.Controls
			If ctl.HasChildren Then
				SetText(ctl, inValue, inItemToFind)
			End If
			If TypeOf (ctl) Is TextBox Then
				If ctl.Tag = inItemToFind Then
					ctl.Text = inValue
				End If
			End If
		Next ctl
	End Sub


This post has been edited by Shadar: 17 November 2010 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#9 Rohella  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 05-November 10

Re: Loop through text boxes to check if they are empty before saving recor

Posted 18 November 2010 - 04:48 AM

When I debug, it seems to skip the following part of the code and goes straight to the second End If. Why is this? its really starting to be confusing :unsure: - :helpsmilie:

If String.IsNullOrEmpty(txtAgentNo.Text) Then
                    MsgBox("Empty text box found")
                    CType(c, TextBox).Focus()
                    Exit For
                End If


Thank you so much, really appreciate this.

p.s you can call me Rose btw, Rohella is my surname :)

Cheers guys

Ooops pasted in the wrong code, that was just something I was trying. There you go guys, cheers :)

          If String.IsNullOrEmpty(CType(c, TextBox).Text) Then
                    MsgBox("Empty text box found")
                    CType(c, TextBox).Focus()
                    Exit For
                End If

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Loop through text boxes to check if they are empty before saving recor

Posted 18 November 2010 - 05:32 AM

2 things to keep in mind
1. If the string contains spaces only. It is not null and not empty. If the flow is moving from the If statement directly to the end if. The condition of the if statement is false. In this case meaning that the textbox it is checking is not nullorempty.
2. Iterating through a For...Each starts from the last control and works it way back to the 1st control so if testing, you may not be checking the textbox you think it's checking.

I don't know if this is Framework 4 specific but there is also a String.IsEmptyOrWhiteSpace method that might work better. Again...I'm not sure at what framework this became available but it should appear in the list along with the .IsNullOrEmpty.

post a screenshot of the layout so that we can check to see if there is something being overlooked as to any other containers that might hold the textboxes.
Was This Post Helpful? 0
  • +
  • -

#11 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Loop through text boxes to check if they are empty before saving recor

Posted 18 November 2010 - 06:41 AM

You don't need the "ctype" statement because you are already looping through all the controls and have an if statement to determine which one is a textbox.

For Each c As Control In Me.Controls  
  If TypeOf c Is TextBox Then  
    If String.IsNullOrEmpty(c.Text) Then  
      MessageBox.Show("Empty Textbox Found")  
      c.Focus()  
      Exit For  
    End If  
  End If  
Next 



PS. Good catch on the spaces Charlie. I didn't need to worry about that on mine but depending on what he is using it for, he might.

This post has been edited by Shadar: 18 November 2010 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 Rohella  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 05-November 10

Re: Loop through text boxes to check if they are empty before saving recor

Posted 30 November 2010 - 04:24 AM

Sorry for late reply guys but iv solved this one :) ... theres a simpler way on Visual Studio 2008. Its called a Required Field Validator, I saw this before but it was validating all the tabs on my page. The solution to this was to changes the "Causes Validation" properties on a function button to false.

Cheers :)

x
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1