Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vbscript if first 2 characters

Status
Not open for further replies.

cumpleby

Technical User
Nov 25, 2011
20
GB
Hi. I'm looking for a script to look at the first 2 characters of a field and match to a defined list and if those 2 characters match 2 characters from a list output some text, if not output some other text.

Example

Field content
22111
33111
44111
55111

List content
22
33
44
55

Thanks
 
What have you tried?

So far your problem does not sound like much of a problem.
cumpleby said:
look at the first 2 characters of a field
If the field's content is contained in a variable "txt", then this is as easy as:
Code:
L2=Left(txt,2)

cumpleby said:
match to a defined list
Let's replace "list" with array and define said array like this:
Code:
Dim arr
arr = Array("s1", "s2", "s3")
Then this could be solved with
Code:
If Ubound(Filter(arr, L2)) > -1 Then

cumpleby said:
if not output some other text
...is then simply the Else part...

Would that do?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thanks For the reply, this is what I have but if doesnt like the ElseIf statement?

Dim Str1, Str2, Field1
Str1 = Array("30", "35", "40", "45", "68", "71", "72", "80", "86", "87", "88", "YY")
Str2 = "RR"
Field1 = ReferenceField("39363895.Field 1")

Label(Left(Field1, 2))

If ubound(Filter(Str1, Label)) Then Response.write "keep refrigerated at <5°C"
ElseIf ubound(Filter(Str2, Label)) Then Response.write "Keep Frozen"
Else Response.write " "
EndIf
 
Label(Left(Field1, 2))
=>
Label = Left(Field1, 2)

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thanks. Ive tried with the change but it still complains about the ElseIf statement:-

The following Script Error was found
(Line9):ElseIf ubound(Filter(Str2, Label)) Then Response.write "Keep Frozen": Syntax error
 
1.) You forgot an essential part:
Code:
Ubound(Filter(arr, L2))[COLOR=#CC0000] > -1[/color]
2.) Str2 is not an array
3.) To run multiple IF/ELSE commands, always use the mulit-line approach:
Code:
If ubound(Filter(Str1, Label))> -1 Then
   Response.write "keep refrigerated at <5°C"
ElseIf Label=Str2 Then
   Response.write "Keep Frozen"
Else
   Response.write " "
End If

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top