How to handle validation errors in vbscript?

Asked 1 month ago
Viewed 7 times

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) & " "


    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..."

asked 1 month ago

Correct Answer

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
answered 1 month ago