Select Case statement with multiple varaibles
am 11.12.2006 06:45:37 von mamun_ah
Hi All,
I wanted to know whether this is possible to use multiple variables to
use in the select case statement such as follows:
select case dWarrExpDateMonth, dRetailDateMonth
case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan"
case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb"
End Select
The reason is I have several date varaibles (mmddyy format) and I want
to display the months in word such as above. Right now I have repeated
the same select case statement for different variables. I was wondering
how can I avoid repeating the select case statement for different
variables which will be nothing but different months.
Thanks a million in advance.
Best regards,
mamun
Re: Select Case statement with multiple varaibles
am 11.12.2006 10:32:51 von Anthony Jones
"microsoft.public.dotnet.languages.vb" wrote in
message news:1165815937.193502.129340@80g2000cwy.googlegroups.com...
> Hi All,
>
> I wanted to know whether this is possible to use multiple variables to
> use in the select case statement such as follows:
>
> select case dWarrExpDateMonth, dRetailDateMonth
> case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan"
> case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb"
> End Select
>
> The reason is I have several date varaibles (mmddyy format) and I want
> to display the months in word such as above. Right now I have repeated
> the same select case statement for different variables. I was wondering
> how can I avoid repeating the select case statement for different
> variables which will be nothing but different months.
>
> Thanks a million in advance.
> Best regards,
> mamun
>
I think you're quite confused as to the Select Case syntax. You should RTM
here:-
http://msdn.microsoft.com/library/en-us/script56/html/91c340 af-8ceb-4f46-86fa-7871eefb3b01.asp
In all VB dialects a new line delimits one statement from another. The
colon : is also such a delimiter allow mulitple statements to appear on one
line. A common use of : in VB is in a Case statement:-
Select Case testExpression
Case expression : somevar = "some literal"
Case expression : somevar = "some other literal"
End Select
but this is just short hand for:-
Select Case testExpression
Case expression
somevar = "some literal"
Case expression
somevar = "some other literal"
End Select
Any case block can contain any number of statements which are only executed
if it's expression it the first one in the list of expressions to match the
testExpression.
Hence this is possilbe:-
Select case dWarrExpDateMonth
Case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan"
Case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb"
End Select
However note that you can only have one test expression.
At guess though I think you may have expected that dWarrExpDateMonth and
dRetailDateMonth to contain differerent values yet be resolved to their
appropriate abbreviation.
In that case a Function is a more appropriate solution:-
Function CvtToMonthAbbrv(rsIn)
Select Case rsIn
Case "01" : CvtToMonthAbbrv="Jan"
Case "02" : CvtToMonthAbbrv="Feb"
End Select
End Function
dWarrExpDateMonth = CvtToMonthAbbrv(dWarrExpDateMonth )
dRetailDateMonth= CvtToMonthAbbrv(dRetailDateMonth)
Having said all that (for the purpose of education) the true solution to
your problem is that VBScript already has such a function built in :-
dWarrExpDateMonth = MonthName(CInt(dWarrExpDateMonth), True)
dRetailDateMonth= MonthName(CInt(dRetailDateMonth), True)
http://msdn.microsoft.com/library/en-us/script56/html/992215 60-8a0a-4f65-a2c2-f263a9bdb241.asp
Anthony.