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
Aspiring Hero
Posts: 2,178
Thanks: 746
Fixes: 45
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 as this was my 3rd ISP in a row that used lithium.
Now you're stuck with me because my new ISP doesn't run a forum Cheesy
Community Veteran
Posts: 38,460
Thanks: 1,027
Fixes: 62
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: 5,237
Thanks: 1,321
Fixes: 31
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
Aspiring Hero
Posts: 2,178
Thanks: 746
Fixes: 45
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 as this was my 3rd ISP in a row that used lithium.
Now you're stuck with me because my new ISP doesn't run a forum Cheesy
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: 5,237
Thanks: 1,321
Fixes: 31
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.