By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,780 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

Looping a Input box

P: 5
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(txtWind.Text) = False Or Val(txtWind.Text) < 0 Or Val(txtWind.Text) > 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
Nov 23 '06 #1
Share this Question
Share on Google+
8 Replies


Expert 5K+
P: 8,434
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.
Nov 23 '06 #2

P: 2
Try this

Dim strText As String

Do
strText = InputBox("Please enter a numeric value", "Title to Input Box")
Loop Until IsNumeric(strText) = 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.
Nov 23 '06 #3

P: 5
Killer,

yeah it is in a Button Click Event.

Nathan G,

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


Soft
Nov 23 '06 #4

Expert 5K+
P: 8,434
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.
Expand|Select|Wrap|Line Numbers
  1. If IsNumeric(txtWind.Text) = False Or Val(txtWind.Text) < 0 Or Val(txtWind.Text) > 25 Then
  2.   MsgBox("Please Enter Positive numbers only!")
  3.   Exit Sub
  4. 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.
Nov 23 '06 #5

100+
P: 267
If IsNumeric(txtWind.Text) = False Or val(txtWind.Text) < 0 Or Val(txtWind.Text) > 25 Then
MsgBox("Please Enter Positive numbers or values smaller than 26 only!")
Exit Sub
End If

???
Nov 23 '06 #6

P: 5
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

Expand|Select|Wrap|Line Numbers
  1.  Do
  2.      strWind = InputBox(" Enter Wind Speed in Miles Per Hour", "Wind Speed")
  3.             If Val(strWind) < 0 Then
  4.                 MsgBox("Please Enter Positive numbers only!")
  5.             End If
  6.  
  7.  
  8.      Loop Until IsNumeric(strWind) = True And Val(strWind) > 0 And Val(strWind) < 25
  9.  
  10.  

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.
Nov 24 '06 #7

P: 5

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
Nov 24 '06 #8

Expert 5K+
P: 8,434
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.
Nov 24 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.