1

I need a VBScript loop asking to entered an integer between 1 to 10 including, if wrong symbol or number entered then asking again until the desired number is retrieved from user.

This is what I tried:

Option Explicit
Dim Num

Num=inputbox("Please enter integer number between 1 to 10")

'Checking if entered  value is numeric
Do while not isnumeric(Num) 
    Num=inputbox("Please enter integer number between 1 to 10", "INCORRECT SYMBOL")
Loop

Do while (Num<1 or Num>10)
    Num=inputbox("Please enter integer number between 1 to 10 ", "Number is NOT IN RANGE") 
Loop

Do while not int(Num)
     Num=inputbox("Please enter integer number between 1 to 10 ", "Number is NOT INTEGER") 
Loop

does not work: when I enter 3 for example I am getting inputbox saying "Number is NOT INTEGER", when entering a letter I receive error message Type mismatch string, error code 800A00D.

3
  • 1
    in what way does it not work? Commented Mar 20, 2015 at 8:15
  • when I enter 3 for example I am getting inputbox saying "Number is NOT INTEGER", when entering a letter I receive error message Type mismatch string, error code 800A00D. I need all the conditions in one function or one loop. But when in one loop then it says : can not perform mathematical actions on string. We had very short VBS training Commented Mar 20, 2015 at 8:29
  • to Soner Gönül : It has to be something like that but it is not working either Option Explicit Dim Num Num=inputbox("Please enter integer number between 1 to 10") 'Checking if entered value is numeric Do while not isnumeric(Num) or (Num<1 or Num>10) Num=inputbox("Please enter integer number between 1 to 10", "INCORRECT SYMBOL") Loop Commented Mar 20, 2015 at 8:35

1 Answer 1

2

You need one Loop. For each (variant) input you need to check:

  1. is it Empty (User pressed Cancel or X) - Abort
  2. is it a blank (zero length or sequence of spaces) string
  3. is it numeric
  4. is it an integer
  5. is it in range - Accept

As in:

Option Explicit

Dim vNum, sNum, nNum
Do
   vNum = InputBox("Please enter an integer beween 1 and 10 (inclusive)")
   If IsEmpty(vNum) Then
      WScript.Echo "Aborted"
      Exit Do
   Else
      sNum = Trim(vNum)
      If "" = sNum Then
         WScript.Echo "Empty string"
      Else
         If IsNumeric(sNum) Then
            nNum = CDbl(sNum)
            If nNum <> Fix(nNum) Then
               WScript.Echo "Not an Integer"
            Else
               If nNum < 1 Or nNum > 10 Then
                  WScript.Echo "Not in range"
               Else
                  WScript.Echo nNum, "is ok"
                  Exit Do
               End If
            End If
         Else
            WScript.Echo "Not a number"
         End If
      End If
   End If
Loop
WScript.Echo "Done"

Using different variables for the different data types may be pedantic, but should show why you had type problems.

Your

Do while not int(Num)

does not work as expected because here Num is a number between 1 and 10; rounding off the (not existing) fractional part gives Num again; Num evaluated in a boolean context/as a bool gives (always) True.

Update wrt comment:

Trim removes space from the head or tail of a string; WScript.Echo sends output to the console (cscript) or a dialog box (wscript).

Update:

As this question shows, I didn't make clear that pressing Cancel or X (Close) sets vNum to an empty variant, which is different from an empty/zero length string. So it should be treated as indication of the users intention to abort.

BTW: You need to read the Docs, but you can't believe them always (cf. here).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You! You explained it very well. Can You tell me please what does "Trim" in "sNum = Trim(vNum)" ? sorry, I don't know how to use mini-Markdown. I am very embarrassed... WScript.Echo is like Inputbox?
Thank You! The more I know, the more I understand how little I know.
Thank You again mister Horner! the only problem, that I am trying to learn HpQTP and it doesn't understand the WScript.Echo so I have to use inputbox instead or something else that I didn't learned about yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.