Results 1 to 13 of 13

Thread: [RESOLVED] Loop until the user inputs a valid answer?

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Resolved [RESOLVED] Loop until the user inputs a valid answer?

    Hi guys. Is it possible for you to loop a block of code until the user provides a valid input? Like, when I click the save button, the program will prompt the user for an integer value, and I would like to validate if the user has inputted a valid value. If the user fails to input a valid value, then the program will re-prompt the same message again until the user inputs a valid value. Eg:

    Code:
    If MsgBox("Add copies to this book?", MsgBoxStyle.YesNo, "Adding Book Copies") = MsgBoxResult.Yes Then
    
    Dim CopyQty As String = InputBox("Amount of copies to generate:", "Add Book Copy")
            If IsNumeric(CopyQty) = False or (CopyQty < 1) Then
                MsgBox("Please key in a valid amount of copies to generate.", MsgBoxStyle.Information, "Invalid Input")
                'Show input box again
            End If
    
    end if
    Last edited by riechan; Feb 16th, 2010 at 07:42 AM.
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Loop until the user inputs a valid answer?

    Use my Numeric Texbox:



    http://www.vbforums.com/showthread.php?t=601413

    Or use custom form:


    I've attached a demo project with this form: (see attached file).

  3. #3
    New Member
    Join Date
    Feb 2010
    Posts
    1

    Re: Loop until the user inputs a valid answer?

    Dim CopyQty As String
    If MsgBox("Add copies to this book?", vbYesNo, "Adding Book Copies") = vbYes Then
    CopyQty = ""

    Do While Val(CopyQty) < 1
    CopyQty = InputBox("Amount of copies to generate:", "Add Book Copy")
    If IsNumeric(CopyQty) = False Or (Val(CopyQty) < 1) Then
    CopyQty = MsgBox("Please key in a valid amount of copies to generate.", vbInformation, "Invalid Input")
    CopyQty = "" 'Show input box again
    End If
    Loop
    End If

    i try this only in Vb6

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Loop until the user inputs a valid answer?

    Use a While loop...

    While answer <> 3

    End While
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Loop until the user inputs a valid answer?

    Quote Originally Posted by Jenner View Post
    Use a While loop...

    While answer <> 3

    End While
    Turn Option Strict On and it will fail.

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Loop until the user inputs a valid answer?

    Quote Originally Posted by cicatrix View Post
    Turn Option Strict On and it will fail.
    Why? It's pseudocode. I'm not even declaring what type of variable "answer" is, though from checking if it's 3, you'd assume it's an integer.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Loop until the user inputs a valid answer?

    Inputbox returns a String. Well, never mind. This remark was not for you but for the OP.

  8. #8

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Loop until the user inputs a valid answer?

    Thanks for the help guys. Here's what I did, using jumer's code as reference:

    Code:
    Dim CopyQty As String
                        CopyQty = ""
    
                        Do While Val(CopyQty) < 1
                            CopyQty = InputBox("Amount of copies to generate:", "Add Book Copy")
                            If IsNumeric(CopyQty) = False Or (Val(CopyQty) < 1) Then
                                MsgBox("Please key in a valid amount of copies to generate.", vbInformation, "Invalid Input")
                                CopyQty = ""
                            End If
                        Loop
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Loop until the user inputs a valid answer?

    Can any of you navigate directly to http://www.vbforums.com/forumdisplay.php?f=25.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] Loop until the user inputs a valid answer?

    Quote Originally Posted by dbasnett View Post
    Can any of you navigate directly to http://www.vbforums.com/forumdisplay.php?f=25.
    I can't... Get account blocked message.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Loop until the user inputs a valid answer?

    Hack messaged me saying that "they" are working on it. "They" are the folks at internet.com

    stanav's avatar seems to about sum it up.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width