# VBSCript If..Else Nested statements



## Yasmin (Jun 1, 1999)

Hi,
This is a very simple example. I'm just beginning to learn VBScript. I keep getting an error message "Expected 'End" or "Expected 'Then" when running this code. It is out of a book and I copied it exactly. It's driving me crazy. Someone please help!!!




```

```


----------



## Yasmin (Jun 1, 1999)

Oops I did'nt mean to post it like that...I meant to show the coding...how can I show the source code?


----------



## IMM (Feb 1, 2002)

Use a couple of ubb tags :
Enclose the portion you want as code with the code and /code tags. (these tags are enclosed within square braces - eg

```
)

See [URL=http://forums.techguy.org/misc.php?action=bbcode]Code Help[/URL]
```


----------



## IMM (Feb 1, 2002)

I think this is what you wanted to post
*************************************************
This is a very simple example. I'm just beginning to learn VBScript. I keep getting an error message "Expected 'End" or "Expected 'Then" when running this code. It is out of a book and I copied it exactly. It's driving me crazy. Someone please help!!!

```
Function ValidInteger(sNumber,iMin,iMax)

    If IsNumeric(sNumber) = 0 Then
        If InStr(sNumber,".") Then
            If CLng(sNumber)>= iMin And CLng(sNumber)<= iMax Then ValidInteger=""
            Else
                ValidInteger = "You must enter a number between " & iMin & " and " & iMax
            End If
        Else
            ValidInteger = "You must enter a whole number"
        End If
    Else
        ValidInteger = "You must enter a number value"
    End If

End Function

Sub cmdCheckForm_onClick
    Dim sValidity
    sValidity = ValidInteger(form1.text1.value,1,10)
    If sValidity = " " Then
        MsgBox "Valid!!!"
    Else
            MsgBox sValidity
    End If
End Sub
```


----------



## Signature (Sep 15, 2001)

I don't know VBScript.
I've probably never even seen it before today.
But, have you tried moving ValidInteger="" to the next line?
Perhaps it's cutting something short, by being placed directly after Then?

I've placed an arrow next to my change below:

```
Function ValidInteger(sNumber,iMin,iMax)

    If IsNumeric(sNumber) = 0 Then
        If InStr(sNumber,".") Then
            If CLng(sNumber)>= iMin And CLng(sNumber)<= iMax Then 
------------->  ValidInteger=""
            Else
                ValidInteger = "You must enter a number between " & iMin & " and " & iMax
            End If
        Else
            ValidInteger = "You must enter a whole number"
        End If
    Else
        ValidInteger = "You must enter a number value"
    End If

End Function

Sub cmdCheckForm_onClick
    Dim sValidity
    sValidity = ValidInteger(form1.text1.value,1,10)
    If sValidity = " " Then
        MsgBox "Valid!!!"
    Else
        MsgBox sValidity
    End If
End Sub
```
Edit: One more thing I noticed - I don't know if it makes a difference, or not - maybe the quotation marks are too close together in ValidInteger=""?
Maybe it should be ValidInteger=" "?


----------



## Yasmin (Jun 1, 1999)

Thanks for your replies. Signature, you were right..part of the mistake was I had to put an enter! but also I had IsNumeric(sNumber) = 0, well the = 0 should have been on the next line InStr(sNumber,".")=0.
I don't think the space btwn the quotes make a difference. I actually tried that too and it worked both ways.
Anyway, this thing drove me crazy but at the same time it was nice to finally solve it! But I dont know if I want to be a programmer everyday!!!
Thanks again!!!


----------



## Signature (Sep 15, 2001)

I've been told, that's what all programmers say:
Sometimes it drives them crazy - but it's so nice when it finally works!
Therefore - you're a natural, Yasmin! 

I was glad to help.


----------

