Looping a Input box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Softstep
    New Member
    • Nov 2006
    • 5

    Looping a Input box

    Hello everyone,

    I am new to this forum, and to programing for that matter. I am a student and taking a very basic intro class in VB 2005.

    I am working on a project that requires a prompt from the user for input <i.e input box> The problem I am having is with trying to validate the inout box after it has failed a Numeric check... The code looks like this


    If IsNumeric(txtWi nd.Text) = False Or Val(txtWind.Tex t) < 0 Or Val(txtWind.Tex t) > 25 Then
    MsgBox("Please Enter Positive numbers only!")
    Else

    When I try and place it in a loop one of 2 things happen. 1) I get tossed into an infintie loop. 2) The program populates the MsgBox and moves on to the next segment of code.

    What I am trying to accomplish is when the IsNumeric check is true it repopulates another InputBox until a Valid input is placed.

    Thanks in Advance
    Soft
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Where is this code located? In a button click event?

    In Visual Basic you tend to do things based on events that occur, not in a set sequence.

    Comment

    • Nathan G
      New Member
      • Nov 2006
      • 2

      #3
      Try this

      Dim strText As String

      Do
      strText = InputBox("Pleas e enter a numeric value", "Title to Input Box")
      Loop Until IsNumeric(strTe xt) = True


      If you have additional requiredments other than just testing if its numeric, add them to the last line, basically it will keep looping until the "Loop Until" line is satisfied.

      Comment

      • Softstep
        New Member
        • Nov 2006
        • 5

        #4
        Killer,

        yeah it is in a Button Click Event.

        Nathan G,

        Thanks i will try that and see if it fixes it.


        Soft

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by Softstep
          yeah it is in a Button Click Event.
          In that case, you should not need to code a loop at all. Just Exit Sub after displaying the messagebox.
          Code:
          If IsNumeric(txtWind.Text) = False Or Val(txtWind.Text) < 0 Or Val(txtWind.Text) > 25 Then
            MsgBox("Please Enter Positive numbers only!")
            Exit Sub
          End If
          No need for an ELSE, because control has now exited from this whole routine back to just waiting for user interaction.

          One other small point - your displayed message doesn't accurately reflect the situation. If I enter "26" it will tell me I must enter a positive number. "But I just did!". It's not good programming practice to confuse your user. That's the sort of thing which makes people hate computers, and the earlier you break the habit, the better.

          Comment

          • albertw
            Contributor
            • Oct 2006
            • 267

            #6
            If IsNumeric(txtWi nd.Text) = False Or val(txtWind.Tex t) < 0 Or Val(txtWind.Tex t) > 25 Then
            MsgBox("Please Enter Positive numbers or values smaller than 26 only!")
            Exit Sub
            End If

            ???

            Comment

            • Softstep
              New Member
              • Nov 2006
              • 5

              #7
              Killer42,

              Thanks for the Advice! I did notice that the message box was wrong when I did a final test of all the input variables. We havent gone beyond looping in VB, and we have yet to touch on anything relating to Subs.
              I did get it to work with some modifictaions based on nathan's example. I will change the MsgBox Before I finish the Project

              Code:
               Do
                   strWind = InputBox(" Enter Wind Speed in Miles Per Hour", "Wind Speed")
                          If Val(strWind) < 0 Then
                              MsgBox("Please Enter Positive numbers only!")
                          End If
              
              
                   Loop Until IsNumeric(strWind) = True And Val(strWind) > 0 And Val(strWind) < 25

              I do have a question though by using the exit Sub how would it loop back into the InputBox? My frist impression is the ExitSub would end that particular line of code and not return to the Loop.. I could be mistaken. I do appreciate clarification if I am wrong.

              Comment

              • Softstep
                New Member
                • Nov 2006
                • 5

                #8
                Originally posted by Softstep

                I do have a question though by using the exit Sub how would it loop back into the InputBox? My frist impression is the ExitSub would end that particular line of code and not return to the Loop.. I could be mistaken. I do appreciate clarification if I am wrong.

                Nevermind I figured it out. Took me a minute.

                Soft

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by Softstep
                  Nevermind I figured it out. Took me a minute.
                  Excellent.

                  Big goof on my part, though - I had been working on the assumption that you were getting text from a textbox on the form, not from the InputBox function. I should read more carefully.

                  Comment

                  Working...