Forum Moderators: open

Message Too Old, No Replies

extremely simple SELECT CASE question

         

rbarkan

4:58 am on Nov 5, 2003 (gmt 0)

10+ Year Member



hiya :)

i'm having trouble performing a very simple VBScript Select Case statement.
to simplify, i need to have something like this:


select case somestring
case "one"
do something
case "two"
do something else
case ""
do nothing!
case else
do something else altogether
end select

i'm having a syntax/understanding(?) problem with HOT TO DO NOTHING?
there's no such thing as exit select and end select ain't right either...

i can't perform the case else unless i check to see that it's not empty.

any help?
10x, r. ;)

duckhunter

5:20 am on Nov 5, 2003 (gmt 0)

10+ Year Member



SELECT FieldName =
CASE somestring
WHEN 'one' THEN '1st String'
WHEN 'two' THEN '2nd String'
ELSE 'Not Found'
END,
anotherfieldfromthetable
FROM YourTable

"FieldName" is the display name given and "somestring" is the column name in YourTable

rbarkan

5:38 am on Nov 5, 2003 (gmt 0)

10+ Year Member



nice try but this is not what i asked for.
i was asking regarding Visual Basic Scripting and not SQL.

10x, r. :)

WebJoe

6:53 am on Nov 5, 2003 (gmt 0)

10+ Year Member



wrap a
if somestring <> "" then ... end if
around your select (just an idea)

rbarkan

8:13 am on Nov 5, 2003 (gmt 0)

10+ Year Member



mmm.. it'll work but i was looking for something more elegant and efficient.
i could also add the IF in the CASE ELSE section, but am looking for a better way to fo it.

cheers, r. :)

Xoc

8:33 am on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



put an apostrophe in front of your "do nothing", as in:

select case somestring
case "one"
call dosomething
case "two"
call dosomethingelse
case ""
'do nothing!
case else
call dosomethingelsealtogether
end select

Case statements in VBScript are mutually exclusive, so if it matches "", it does not fall through into the case else. I like to comment these so it's clear. They are also evaluated in order so that if two case statements match the criteria, only the first is executed. For example:

Select case 3
Case 1 to 4
Call DoA
Case 2 to 5
Call DoB
End Select

DoA will get called, not DoB.

rbarkan

8:56 am on Nov 5, 2003 (gmt 0)

10+ Year Member



many thanks, this is almost what i needed to hear.. :)

well, i must write some statement mustn't i?
not writing anything between the CASE "" and CASE ELSE lines, or writing a commented line doesn't seem to work.

r. :)

Xoc

9:16 am on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you don't have to write anything. I like a comment, but it isn't required.

rbarkan

8:13 pm on Nov 5, 2003 (gmt 0)

10+ Year Member



the CASE "" with no following statement seems to simply fall through to the CASE ELSE.

any suggestions? :-?

Xoc

8:52 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then it means that the variable is not matching the "". I strongly suspect that your variable contains Empty, not "". Those are two different values.

aspdaddy

9:29 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A tip I learned here a long time ago is to append vbScript strings with an empty string, before doing any tests for empty.

str = str & ""

No more null values :)

If somestring & "" <> "" then
If somestring ="one" then
do something
else if somestring ="two" then
do something else
else
do something else altogether
end if
end if

Zaphod Beeblebrox

12:26 pm on Nov 6, 2003 (gmt 0)

10+ Year Member



Just leave the Case "" out of the whole matter. If you don't want any action, leave it out.

If you want to exclude it from the Case Else situation, you should put it in, without anything following it, so:


Case "one"
'Do one stuff
Case ""
Case Else
'Do other stuff

is perfectly valid and should work.

rbarkan

2:35 pm on Nov 6, 2003 (gmt 0)

10+ Year Member



thanks alot you guys!

yep, i forgot to append with:
somestring = somestring & ""

silly me,
thanks again!
ronnie :)

rbarkan

2:44 pm on Nov 6, 2003 (gmt 0)

10+ Year Member



btw, just for the records:
my string was NULL since its value was retrieved from a database. since there was no text in that field, the DB returned NULL.
(have been using the RS.GetRows() method)

cheers, r. :))

Zaphod Beeblebrox

10:05 am on Nov 7, 2003 (gmt 0)

10+ Year Member



I always use a function called FToStr:


Function FToStr(Fld)
If IsNull(Fld) Then
FToStr = ""
Else
FToStr = CStr(Fld)
End If
End Function
Dim TestString
Select Case FToStr(Rs("Fieldname"))