cancel
Showing results for 
Search instead for 
Did you mean: 

VBScript - IF something equals something

FIXED
Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

VBScript - IF something equals something

i have a simple IF THEN ELSE

IF variable1 = "A" AND variable2 = "B" THEN
ELSE
END IF

and that works but appears to be case sensitive.
so if variable2 = "b" then it fails.

is there any way to make it so that it'll pass if variable2 is B or b?
(this is just a simplifed version, but variable2 could be a word and not just a letter.

8 REPLIES
Browni
Seasoned Champion
Posts: 1,607
Thanks: 457
Fixes: 36
Registered: 02-03-2016

Re: VBScript - IF something equals something

https://www.w3schools.com/asp/func_ucase.asp
I must have been really bad in a previous life. This is my 3rd ISP in a row that uses lithium.
Community Veteran
Posts: 38,307
Thanks: 972
Fixes: 57
Registered: 15-06-2007

Re: VBScript - IF something equals something

this gives two solutions - the one above and the other using string comparison

https://blogs.technet.microsoft.com/heyscriptingguy/2005/06/22/how-can-i-compare-two-string-values-r...

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: VBScript - IF something equals something

how is converting the variable to uppercase going to help?
that would only help if both items being compared were in upper case, which they might not be.

the IF statement needs to be case insensitive.

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: VBScript - IF something equals something


Oldjim wrote:

this gives two solutions - the one above and the other using string comparison

https://blogs.technet.microsoft.com/heyscriptingguy/2005/06/22/how-can-i-compare-two-string-values-r...


i actually looked at that very page, but came to the conclusion that StrComp may not work 100% of the time.

Community Veteran
Posts: 4,978
Thanks: 1,153
Fixes: 28
Registered: 16-10-2014

Re: VBScript - IF something equals something

Fix

You can always use :

IF (strcomp(Var2, "B", vbTextCompare) == 0) THEN
...
END IF

When the value returned by the function is < 0 then the Var2 is less than "B" (i.e. A -v- B) when its > than 0 then Var2 is greater than "B" (C -v- B) but when it's 0 then both string are deemed the same regardless of case.

 Edit:- Correct type and added this:

Why do you say it won't work 100% of the time?

Browni
Seasoned Champion
Posts: 1,607
Thanks: 457
Fixes: 36
Registered: 02-03-2016

Re: VBScript - IF something equals something

if ucase(variable1) = ucase(variable2)

or

if ucase(variable) = "A"

I must have been really bad in a previous life. This is my 3rd ISP in a row that uses lithium.
Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: VBScript - IF something equals something


Mook wrote:

You can always use :

IF (strcomp(Var2, "B", vbTextCompare) == 0) THEN
...
END IF

When the value returned by the function is < 0 then the Var2 is less than "B" (i.e. A -v- B) when its > than 0 then Var2 is greater than "B" (C -v- B) but when it's 0 then both string are deemed the same regardless of case.

 Edit:- Correct type and added this:

Why do you say it won't work 100% of the time?


what if it is "b" though (as opposed to "B") ?
or what if Var2 is "SomeThing" and it's comparing it against "something" or "someThing" or "somethinG" ? etc etc

Community Veteran
Posts: 4,978
Thanks: 1,153
Fixes: 28
Registered: 16-10-2014

Re: VBScript - IF something equals something

Well due to the way the function works an implied case conversion is done to the input parameters, so it can always guarantee you consistent results. By using this method you don’t have to worry about the case of either input as the functions will apply its own logic to deal with that. So take that into consideration when or if you use it.

If Var2 is ‘SomeThing’ then comparing it against ‘something’ or ‘someTHing’ will produce a 0. However if you compare it against ‘somethings’ then you’d get value > that 0.