Wrox Programmer Forums
|
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2011, 12:12 PM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stuck in a Loop

Hi there,

I have a user interface that I use to enter employee data including socials which are stored as a string. When validating the data, I'm getting stuck in a loop. Basically, if a user doesn't enter anything in the box or if the data isn't a digit, an input box pops up and asks them to reenter the data. However, even if the user enters the appropriate number of characters, they get stuck in a loop.

Private Sub saveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveButton.Click

Dim SSN As String
Dim SSN_COPY As String
Dim SSN_FIRST As String
Dim SSN2 As String
Dim SSN_LAST As String
Dim SSN_MID As String

If ssnTextBox.Text = String.Empty Then
Do
SSN = InputBox("Enter a 9-digit social security number (no dashes)", "Soc Sec Number")
Loop Until SSN.Length = 9 And (ssnTextBox.Text Like "#########")

ElseIf ssnTextBox.Text.Length <> 9 Then
Do
SSN = InputBox("Enter a 9-digit social security number (no dashes)", "Soc Sec Number")
Loop Until SSN.Length = 9 And (ssnTextBox.Text Like "#########")

ElseIf Not (ssnTextBox.Text Like "#########") Then
Do
SSN = InputBox("Enter a 9-digit social security number (no dashes)", "Soc Sec Number")
Loop Until ssnTextBox.Text.Length = 9 And (ssnTextBox.Text Like "#########")

End

Else
SSN = ssnTextBox.Text
SSN_COPY = SSN
SSN_FIRST = SSN_COPY.Remove(3, 6)
SSN_LAST = SSN_COPY.Remove(0, 5)
SSN2 = SSN_COPY
SSN_COPY = Mid(SSN2, 4, 2)
SSN_MID = SSN_COPY

ssnTextBox.Text = (SSN_FIRST & "-" & SSN_MID & "-" & SSN_LAST)
End If

Any suggestions? Thanks!

Trixie
 
Old May 11th, 2011, 12:31 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Trixie,

It looks like the test to end the loop is looking at the newly entered SSN value to see if it has length 9 but then it also looks at the TextBox's Text to see if it has the format #########. If the TextBox initially had some other format, then that won't change when the user enters a value in the InputBox. You need it to test the new value in SSN instead of the TextBox.

I think I would replace the whole If Then Else sequence with something like this:

Code:
        Dim ssn As String = ssnTextBox.Text

        Do While (ssn.Length <> 9) OrElse (Not (ssn Like "#########"))
            ssn = InputBox( _
                "Enter a 9-digit social security number (no dashes)", _
                "Soc Sec Number", ssn)
        Loop
Let me know if that doesn't get you going again.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old May 11th, 2011, 12:46 PM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm no longer stuck in the loop and my input box works correctly if a user enters an alpha character but the information is not displaying correctly in my textbox. The original data is staying in there instead.

Dim SSN As String = ssnTextBox.Text
Dim SSN_COPY As String
Dim SSN_FIRST As String
Dim SSN2 As String
Dim SSN_LAST As String
Dim SSN_MID As String


If ssnTextBox.Text = String.Empty Or SSN.Length <> 9 Then
Do While (SSN.Length <> 9) OrElse (Not (SSN Like "#########"))
SSN = InputBox("Enter a 9-digit social security number (no dashes)", _
"Soc Sec Number", SSN)
Loop
Else

SSN_COPY = SSN
SSN_FIRST = SSN_COPY.Remove(3, 6)
SSN_LAST = SSN_COPY.Remove(0, 5)
SSN2 = SSN_COPY
SSN_COPY = Mid(SSN2, 4, 2)
SSN_MID = SSN_COPY

ssnTextBox.Text = (SSN_FIRST & "-" & SSN_MID & "-" & SSN_LAST)
End If
 
Old May 11th, 2011, 12:58 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

You have all of the code that splits apart the SSN in the Else part of the If Then Else statement so it only executes if the user enters a valid SSN to being with. You should probably move that code outside of the If Then Else statement. (And remove the Else part entirely.)
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old May 11th, 2011, 01:02 PM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That worked! Thank you so much. I've been working on this for hours trying to get it to work properly. Have a great day and thanks again for your help!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stuck in chapter 4 louisvillegeorge BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 2 June 17th, 2009 10:54 PM
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
IM STUCK!!! cagaroos BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 10 December 31st, 2008 06:22 PM
Stuck on the first chapter flashster BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 September 1st, 2006 03:40 AM
I'm stuck! budman Classic ASP Databases 2 November 3rd, 2003 12:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.