Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case Range - Easy One I'm sure

Status
Not open for further replies.

dcwave

IS-IT--Management
May 18, 2003
46
US
Okay.. here's an easy one...
Code:
Function result(Number)

Select Case Number
  Case 1 to 5
   result = "hi"
  Case 6 to 10
   result = "Bye"
end Select
End Function

I get the "expected statement" error.
I think this has to do with the "to".
How do I do a range in VBscript/ASP?

Thanks


 
The code looks alright. Maybe you miss Case Else?

Function result(Number)
Select Case Number
Case 1 to 5
result = "hi"
Case 6 to 10
result = "Bye"
Case Else
result = "Unexpected"
End Select
End Function
 
Here's an example using States and Countries that might help:

SELECT
CASE st
WHEN 'AL' THEN 'Alabama'
WHEN 'AR' THEN 'Alaska'
WHEN 'AZ' THEN 'Arizona'
WHEN 'CA' THEN 'California'
WHEN 'CO' THEN 'Colarodo'
WHEN 'FL' THEN 'Florida'
WHEN 'GA' THEN 'Georgia'
WHEN 'HI' THEN 'Hawaii'
WHEN 'IA' THEN 'Iowa'
WHEN 'ID' THEN 'Idaho'
WHEN 'IL' THEN 'Illinois'
WHEN 'IN' THEN 'Indiana'
WHEN 'KY' THEN 'Kentucky'
WHEN 'LA' THEN 'Louisana'
WHEN 'MD' THEN 'Maryland'
WHEN 'MI' THEN 'Michigan'
WHEN 'MN' THEN 'Minnesota'
WHEN 'MO' THEN 'Missouri'
WHEN 'MS' THEN 'Mississippi'
WHEN 'MT' THEN 'Montana'
WHEN 'NC' THEN 'North Carolina'
WHEN 'ND' THEN 'North Dakota'
WHEN 'NE' THEN 'Nebraska'
WHEN 'NM' THEN 'New Mexico'
WHEN 'NV' THEN 'Nevada'
WHEN 'NY' THEN 'New York'
WHEN 'OH' THEN 'Ohio'
WHEN 'OK' THEN 'Oklahoma'
WHEN 'OR' THEN 'Oregon'
WHEN 'SC' THEN 'South Carolina'
WHEN 'SD' THEN 'South Dakota'
WHEN 'TN' THEN 'Tennessee'
WHEN 'TX' THEN 'Texas'
WHEN 'UT' THEN 'Utah'
WHEN 'WA' THEN 'Washington'
WHEN 'WI' THEN 'Wisconsin'
ELSE
CASE country
WHEN 'AU' THEN 'Australia'
WHEN 'GB' THEN 'Great Britian'
WHEN 'NZ' THEN 'New Zealand'
WHEN 'MX' THEN 'Mexico'
WHEN 'US' THEN 'Missing State'
ELSE 'Unknown'
END
END,
st, Country
FROM table_name
GROUP BY st, country
ORDER BY st
 
you can also :

select case number
case 1,2,3,4,5
blah
case 6,7,8,9,10
blah2
End select

OR

select case blah
case 1<=number and number<=5
dosomething
case 6<=number and number<=10
dosomethingelse
end select

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
VBScript does not support ranges or comparators, as it handles values by comparing the original value to a single value or list of values in each case statement. This is unfortunate but there is a way around it.

dcwave's method doesn't work because the syntax isn't supported. I think bhuning was doing an SQL-based SELECT case which is a differant syntax then VBScript. Drex's will loook like it is working, but it won't be (not really).

in fact, Drex's might be a good example. Since ASP is comparing strictly value to value (unlike VB6, etc that can handle ranges or the IS keyword) then what happens is that it evaluates each case argument asa single value. In Drex's we have:
Select case blah
case 1<=number and number<=5
etc

When VBScript goes to start making comparisons it looks at that case argument and asks if blah is = to (1<=number and number<=5). What happens is the case part gets evaluated down to a boolean value, so you are then comparing your variable to a boolean value. Here's a simple one you can paste andrun yourself:
Code:
Dim a
a = 4
Select Case a
   Case a >= 0 And a < 3
      response.Write "0-3"
   Case a >= 3 and a < 5
      Response.Write "3-5"
   Case a >= 5 and a < 7
      Response.Write "5-7"
   Case Else
      Response.Write "Out of bounds!"
End Select
At first glance you would think the "3-5" would be hit, but if we evaluate all the argumebnts down to single values (like VBScript would) we'll see why it would hit the Else here everytime:
Code:
Code Becomes:
Dim a
a = 4
Select Case a
   Case False
      response.Write "0-3"
   Case True
      Response.Write "3-5"
   Case False
      Response.Write "5-7"
   Case Else
      Response.Write "Out of bounds!"
End Select

a won't match false or true, so it ends up being kicked to the else statement.


Solution: A hack I came upo with sometime back that hasn't shown up recently is to Select case on True, but continue to have your expressions in the case statement. This way you can still do comparitive ranges but your not forced to the Else every tme:
Code:
Dim a
a = 4
Select Case [highlight]True[/highlight]
Case a >= 0 And a < 3
      response.Write "0-3"
   Case a >= 3 and a < 5
      Response.Write "3-5"
   Case a >= 5 and a < 7
      Response.Write "5-7"
   Case Else
      Response.Write "Out of bounds!"
End Select

Now the first statement that evaluates to true is going to match the original value in your Select case and everything will be good. So instead of having to tpe all the possible values next to your Case you can actually use ranges, since your trying to match boolean to boolean rather then number to boolean.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top