'How to handle validation errors in vbscript?
How to handle validation errors to string in vbscript? I don't understand why if I type a letter my code doesn't return to the loop
Dim mark
Dim ask
mark = Chr(10) & " " & Chr(149) & " "
Do
ask = InputBox("Select:" & Chr(10) & mark & "1) First" & mark & "2) Second" & mark & "3) All" & Chr(10), "Select a option")
If IsEmpty(ask) Then WScript.Quit 'Read if canceled
If IsNumeric(ask) and CStr(CLng(ask)) = ask and ask => 1 and ask <= 3 Then Exit Do
MsgBox "Choice a valid value", 48, "Ops..."
Loop
Solution 1:[1]
In case of a character, your If statement will generate an error because you are converting ask (which is a String in this scenario) to a Long (CLng(ask)). You should check if ask is numeric and then, in another statement, check the value:
If IsNumeric(ask) Then
If CStr(CLng(ask)) = ask And ask >= 1 And ask <= 3 Then Exit Do
End If
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Étienne Laneville |
