×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Vbscript if first 2 characters

Vbscript if first 2 characters

Vbscript if first 2 characters

(OP)
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

RE: Vbscript if first 2 characters

What have you tried?

So far your problem does not sound like much of a problem.

Quote (cumpleby)

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) 

Quote (cumpleby)

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 

Quote (cumpleby)

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.

RE: Vbscript if first 2 characters

(OP)
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

RE: Vbscript if first 2 characters

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.

RE: Vbscript if first 2 characters

(OP)
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

RE: Vbscript if first 2 characters

1.) You forgot an essential part:

CODE

Ubound(Filter(arr, L2)) > -1 
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.

RE: Vbscript if first 2 characters

(OP)
Perfect, that's got it, thank you.

RE: Vbscript if first 2 characters

Can someone come help me on my thread?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close