Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

CASE Statement using the LIKE comparitive

I have a value stored in the first element of an array: ScriptStart(0).  I want to select the case based on if the first element's string is "LIKE" one of the Cases.  I don't know the proper syntax to accomplish this.  Here's what I'm trying (with no luck):

Sub CaseLike()
'--- As you can see below there is no CASE for "peg_sqr2"
'--- but there is a CASE for "peg_sqr"
'--- so I need help with a LIKE statement
Dim ScriptStart(5)
ScriptStart(0) = "peg_sqr2"

Select Case ScriptStart(0)

Case Like "peg_leg*", "peg_ae*", "peg_flat*"
MsgBox "It works"

Case Like "peg_sqr*", "peg_ldg*"
MsgBox "It works"

Case Like "peg_cbl*"
MsgBox "It works"

End Select
End Sub
0
jbauer22
Asked:
jbauer22
1 Solution
 
alaplumeCommented:
Thy something like this instead:

if instr(1,ScriptStart(0),"peg_leg")>0 _or
   instr(1,ScriptStart(0),"peg_ae")>0 _or
   instr(1,ScriptStart(0),"peg_flat")>0 then

   msgbox "It Works"

elseif ...
.
.
.
end if

0
 
jbauer22Author Commented:
That's an alternative but I'd really like to know if the the LIKE comparitive can be used with CASE.
0
 
Éric MoreauSenior .Net ConsultantCommented:
Not really but yes!!! The real advantage of a SELECT CASE is to evaluate an expression once and then compare it to a series of value. The Like operator cannot be used in this scenario.

But you can use the SELECT CASE if you really want to. You need to do a "Select Case True".

Sub CaseLike()
'--- As you can see below there is no CASE for "peg_sqr2"
'--- but there is a CASE for "peg_sqr"
'--- so I need help with a LIKE statement
Dim ScriptStart(5)
ScriptStart(0) = "peg_sqr2"

Select Case True

Case ScriptStart(0) Like "peg_leg*", ScriptStart(0) Like "peg_ae*", ScriptStart(0) Like "peg_flat*"
MsgBox "1- It works"

Case ScriptStart(0) Like "peg_sqr*", ScriptStart(0) Like "peg_ldg*"
MsgBox "2- It works"

Case ScriptStart(0) Like "peg_cbl*"
MsgBox "3- It works"

End Select
End Sub
0
 
Dave_GreeneCommented:
Best you will be able to do in this case will be ...

Select Case Left(ScriptStart(0), 7)

Case "peg_leg", "peg_cbl", "peg_sqr", "peg_ldg"
   MsgBox "It works"
Case Else
   Select Case Left(ScriptStart(0), 6)
      Case "peg_ae",
         Msgbox "6 Works"
      Case Else
          Select Case Left(ScriptStart(0), 8)
               Case "peg_flat"
                    MsgBox "8 works"
               Case Else
                    msgbox "Not Found
          End Select
    End Select
End Select
0
 
jbauer22Author Commented:
That's what I'm talking about.  Thanks!  A+
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now