Reputation: 59
Is there an equivalent of the IN
Statement for VBScript?
I am looking to create an If
statement for column visibility which checks if a particular value was selected in a parameter. This value is multi-valued so I need to test if it's present in a list of values.
I have a lookup table which assigns a value to 4 different call types:
SELECT 1 as CALL_ORDER,'Welcome Call' AS [TEXT], '1' AS [VALUE]
UNION
SELECT 2 as CALL_ORDER,'Month One Call' AS [TEXT], '2' AS [VALUE]
UNION
SELECT 3 as CALL_ORDER,'Month Two Call' AS [TEXT], '3' AS [VALUE]
UNION
SELECT 4 as CALL_ORDER,'Month Six Call' AS [TEXT], '4' AS [VALUE]
visual studio :
The user has an option to select a multiple number of these options and I need to test if, when run, a specific value is present in the list.
For example:
If 1 IN(PARAMETERS!CALL_TYPE.VALUE) Then ...
Not sure how you would do this in VBScript.
Upvotes: 0
Views: 2757
Reputation: 79
I would simply use the InStr function. E.g. I want to solve something like this: a = 2 ....Where a in (12,23,45)
Then I'd use a = Cstr(2) ... if Instr("12,23,45",a)>0 then ...
Upvotes: -1
Reputation: 200293
VBScript doesn't have an In
or Contains
statement/operator.
I think the most common way to do In
or Contains
checks in VBScript is to build a dictionary with the possible values and check the actual value against that dictionary:
Set ref = CreateObject("Scripting.Dictionary")
ref.Add "foo", True
ref.Add "bar", True
var = "foo"
If ref.Exists(var) Then 'value exists
'...
End If
var = "baz"
If ref.Exists(var) Then 'value doesn't exist
'...
Else
'...
End If
Another common approach is to use multiples of 2 as flags in an integer value:
Const FOO = 1
Const BAR = 2
Const BAZ = 4
var = 5
If (var And FOO) = FOO Then 'value exists
'...
End If
If (var And BAR) = BAR Then 'value doesn't exist
'...
Else
'...
End If
Depending on your actual requirements you may also be able to use a Select Case
statement:
Select Case var
Case "foo": 'do some
Case "bar": 'do other
Case Else: 'everything else
End Select
Upvotes: 1