cancel
Showing results for 
Search instead for 
Did you mean: 

VBScript - IF something equals something

FIXED
chenks76
All Star
Posts: 3,274
Thanks: 338
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 8
Browni
Aspiring Hero
Posts: 2,673
Thanks: 1,054
Fixes: 60
Registered: ‎02-03-2016

Re: VBScript - IF something equals something

Oldjim
Resting Legend
Posts: 38,460
Thanks: 787
Fixes: 63
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...

chenks76
All Star
Posts: 3,274
Thanks: 338
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.

chenks76
All Star
Posts: 3,274
Thanks: 338
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.

Anonymous
Not applicable

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,673
Thanks: 1,054
Fixes: 60
Registered: ‎02-03-2016

Re: VBScript - IF something equals something

if ucase(variable1) = ucase(variable2)

or

if ucase(variable) = "A"

chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: VBScript - IF something equals something


@Anonymous 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

Anonymous
Not applicable

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.