Select Case in VBScript

Use Select Case in Classic ASP (VBScript) when one value must be checked against several possible options. The matching Case runs its statements; Case Else handles unexpected values.

Select Case example: match a number

Dim my_num
my_num = 150
Select Case my_num
    Case 100
        Response.Write "It is equal to 100"
    Case 150
        Response.Write "It is equal to 150"
    Case 200
        Response.Write "It is equal to 200"
    Case Else
        Response.Write "No matching value"
End Select

Output:

It is equal to 150

The number 150 matches the second Case, so only its message is displayed. Select Case is a readable alternative to several If...ElseIf conditions when all branches examine the same expression.

Select Case syntax and execution order

Select Case expression
    Case value1
        ' statements when expression = value1
    Case value2, value3
        ' statements when expression = value2 or value3
    Case Else
        ' statements when nothing matches
End Select
VBScript statementPurpose
Select Case expressionEvaluates the value to test. It may be a number, string, or expression.
Case valueMatches one value.
Case value1, value2Matches either of the comma-separated values.
Case ElseOptional fallback if none of the earlier cases match.
End SelectEnds the decision block.

The first matching Case wins. After its statements run, execution continues after End Select; unlike some languages, VBScript does not need a break statement after each branch. If no case matches and there is no Case Else, the block produces no action.

Original example: compare 100, 150 and 200

The original tutorial compares my_num = 150 with three numeric choices. Here it is with consistent indentation and the same output.

Dim my_num
my_num = 150
Select Case my_num
    Case 100
        Response.Write "It is equal to 100" & "<br>"
    Case 150
        Response.Write "It is equal to 150" & "<br>"
    Case 200
        Response.Write "It is equal to 200" & "<br>"
End Select

Output:

It is equal to 150

If my_num were 999, this example would display nothing, because it has no matching branch and no fallback.

Match multiple values in a single Case

A comma separates alternatives. This keeps the logic short when two or more values should trigger the same response.

Dim my_num
my_num = 160
Select Case my_num
    Case 100
        Response.Write "It is equal to 100"
    Case 150, 160
        Response.Write "It is equal to 150 or 160"
    Case Else
        Response.Write "Other number"
End Select

Output:

It is equal to 150 or 160

Match several text values

Dim answer
answer = "y"
Select Case answer
    Case "yes", "y"
        Response.Write "Confirmed"
    Case "no", "n"
        Response.Write "Declined"
    Case Else
        Response.Write "Enter yes or no"
End Select

Output:

Confirmed

A Case list checks for any of the specified values. It does not test whether part of a string contains a word; for substring searches, use InStr().

Handle unmatched values with Case Else

The original page uses my_num = 160 while its listed cases are 100 and 150. The fallback therefore runs.

Dim my_num
my_num = 160
Select Case my_num
    Case 100
        Response.Write "It is equal to 100" & "<br>"
    Case 150
        Response.Write "It is equal to 150" & "<br>"
    Case Else
        Response.Write "It is not equal to any listed value" & "<br>"
End Select

Output:

It is not equal to any listed value

Place Case Else after the other cases, and use it for unexpected input rather than silently accepting unknown values.

Select Case with strings and input normalization

When a visitor may enter uppercase letters or surrounding spaces, normalize the text before checking the options. LCase() converts text to lowercase and Trim() removes surrounding spaces.

Dim status, message
status = "  ACTIVE  "
Select Case LCase(Trim(status))
    Case "active"
        message = "Account is active"
    Case "pending"
        message = "Awaiting approval"
    Case "closed"
        message = "Account is closed"
    Case Else
        message = "Unknown account status"
End Select
Response.Write message

Output:

Account is active

Use one Case for several statuses

Dim status
status = "processing"
Select Case LCase(Trim(status))
    Case "new", "processing", "packed"
        Response.Write "Order in progress"
    Case "shipped", "delivered"
        Response.Write "Order on its way or completed"
    Case Else
        Response.Write "Status not recognized"
End Select

Output:

Order in progress

Using LCase() makes the accepted casing explicit; this matters when text comes from a submitted ASP form.

Numeric ranges in VBScript: Select Case True

Classic ASP compatibility: VBScript does not support VBA-specific Case 1 To 10 or Case Is > 10 syntax. For ranges, evaluate Boolean conditions using Select Case True, or use If...ElseIf...Else.

In the following example, every Case condition returns True or False. The first True case provides the grade.

Dim marks, grade
marks = 78
Select Case True
    Case marks >= 90 And marks <= 100
        grade = "A"
    Case marks >= 75 And marks < 90
        grade = "B"
    Case marks >= 60 And marks < 75
        grade = "C"
    Case marks >= 0 And marks < 60
        grade = "Needs improvement"
    Case Else
        grade = "Marks must be from 0 to 100"
End Select
Response.Write grade

Output:

B

Keep the ranges in a sensible order, especially if some conditions overlap. Here marks = 78 meets the 75–89 condition. Each comparison is written out explicitly so the boundaries are unambiguous.

Check even and odd numbers

Dim number
number = 12
Select Case number Mod 2
    Case 0
        Response.Write "Even number"
    Case 1
        Response.Write "Odd number"
End Select

Output:

Even number

For nonnegative whole numbers, Mod 2 gives 0 for even and 1 for odd values. Use If to validate any visitor-entered number before applying numeric operations.

Nested Select Case and looping through values

Nested Select Case

Use a second Select Case inside a branch if a decision depends on another option.

Dim paymentMethod, currency
paymentMethod = "card"
currency = "USD"
Select Case paymentMethod
    Case "card"
        Select Case currency
            Case "USD"
                Response.Write "Card payment in US dollars"
            Case "INR"
                Response.Write "Card payment in rupees"
            Case Else
                Response.Write "Unsupported card currency"
        End Select
    Case "cash"
        Response.Write "Cash payment"
    Case Else
        Response.Write "Unknown payment method"
End Select

Output:

Card payment in US dollars

Select Case inside a For Each loop

You can classify each item in a VBScript array using the same decision block.

Dim statuses, status
statuses = Array("new", "pending", "done")
For Each status In statuses
    Select Case status
        Case "new"
            Response.Write "New order<br>"
        Case "pending"
            Response.Write "Waiting<br>"
        Case "done"
            Response.Write "Completed<br>"
        Case Else
            Response.Write "Other<br>"
    End Select
Next

Output:

New order
Waiting
Completed

The For Each loop visits the values in sequence and runs one branch for each. The <br> tags create line breaks in the rendered page.

Complete Classic ASP example: choose a category

Save the following code in a separate select-case-form.asp test file and open it on an IIS server with Classic ASP enabled. This example accepts a POST value, normalizes it, selects a message, and HTML-encodes displayed text.

<%@ Language="VBScript" %>
<% Option Explicit %>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Select Case Form</title></head>
<body>
<h1>Choose a category</h1>
<form method="post" action="">
    <label for="category">Category:</label>
    <select name="category" id="category">
        <option value="books">Books</option>
        <option value="games">Games</option>
        <option value="toys">Toys</option>
    </select>
    <button type="submit">Show category</button>
</form>
<%
Dim selectedCategory, message
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    selectedCategory = LCase(Trim(Request.Form("category")))
    Select Case selectedCategory
        Case "books"
            message = "Browse our books"
        Case "games"
            message = "Browse our games"
        Case "toys"
            message = "Browse our toys"
        Case Else
            message = "Please choose a valid category"
    End Select
    Response.Write "<p>" & Server.HTMLEncode(message) & "</p>"
End If
%>
</body>
</html>

The Case Else branch is still needed even though the visible dropdown has only three options: an HTTP client can submit a different value. Validate all server-side input before using it in queries or other operations.

Classify a validated number submitted by a form

For ranges, first make sure a submitted value is numeric. This standalone VBScript segment assumes it is placed inside a <% ... %> block on a Classic ASP page.

Dim rawMarks, marks, grade
rawMarks = Trim(Request.Form("marks"))
If IsNumeric(rawMarks) Then
    marks = CDbl(rawMarks)
    Select Case True
        Case marks < 0 Or marks > 100
            grade = "Invalid marks"
        Case marks >= 75
            grade = "Pass with distinction"
        Case marks >= 40
            grade = "Pass"
        Case Else
            grade = "Needs improvement"
    End Select
Else
    grade = "Enter a numeric mark"
End If
Response.Write Server.HTMLEncode(grade)

This illustrates input validation and conditional classification. Decimal conversion depends on the server locale; use a predictable input format and explicit validation when the exact numeric representation matters.

Troubleshooting and frequently asked questions

IssueWhat to check
No outputNo Case matched and Case Else is missing. Add a fallback or check the input value.
Unexpected branchOnly the first matching case runs. Check ordering and overlapping conditions.
Case does not match typed textCheck whitespace, letter case, and the source of the submitted value; use LCase(Trim(...)) when appropriate.
Case 1 To 10 causes a syntax errorThat VBA-style range is not supported in VBScript. Use Select Case True with a Boolean comparison or use If...ElseIf.
Numeric comparison errorValidate with IsNumeric() before attempting CDbl() or numeric comparisons.
Page shows the literal ASP sourceRun a .asp page through IIS with Classic ASP enabled, rather than opening the file directly in a browser.

Does Select Case run multiple matching branches?

No. When more than one Case could match, only the first matching branch runs. Processing then continues after End Select.

Is Case Else required?

No. It is optional, but it is useful for invalid, missing, and unrecognized values. Without it, an unmatched value runs no branch.

Can one Case match several values?

Yes. Separate values with commas, for example Case 150, 160 or Case "yes", "y".

Can VBScript Select Case use comparisons such as greater than or less than?

Yes, by using Select Case True with conditions such as Case marks >= 75. Do not copy Case Is > 75 from VBA examples into Classic ASP VBScript.

What is the difference between Select Case and If...ElseIf?

Select Case is a good fit when you are evaluating one expression against many alternatives. If...ElseIf is often clearer when unrelated conditions, complex logic, or several variables need to be tested.

Do I need to add break after each Case?

No. VBScript does not fall through to the next branch. Do not add a JavaScript- or PHP-style break statement.

For further syntax details, see the VBScript Select Case reference.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer