Using Instr with Case

  • Thread starter Thread starter scott
  • Start date Start date
S

scott

I'm trying to use an "Instr" test with a case statement. Is this possible?

In my code below, I'm trying to test the first 2 characters in a string and
determine if the string begins with "f_" or "r_". Can someone take a stab at
my code? It returns "unknown" instead of "f_"?


CODE: ***************************


Public Sub TestCase()
Dim sString As String

sString = "f_test1"

Select Case sString

Case InStr(2, sString, "f_") > 0
Debug.Print "Caught f_"
Case InStr(2, sString, "r_") > 0
Debug.Print "Caught r_"
Case Else
Debug.Print "Unknown"
End Select

End Sub
 
scott said:
I'm trying to use an "Instr" test with a case statement. Is this
possible?
In my code below, I'm trying to test the first 2 characters in a
string and determine if the string begins with "f_" or "r_". Can
someone take a stab at my code? It returns "unknown" instead of "f_"?


CODE: ***************************


Public Sub TestCase()
Dim sString As String

sString = "f_test1"

Select Case sString

Case InStr(2, sString, "f_") > 0
Debug.Print "Caught f_"
Case InStr(2, sString, "r_") > 0
Debug.Print "Caught r_"
Case Else
Debug.Print "Unknown"
End Select

End Sub

Case doesn't work the way you are trying to make it. You can get the same
result with the following syntax...

Public Sub TestCase()
Dim sString As String

sString = "f_test1"

Select Case True
Case InStr(2, sString, "f_") > 0
Debug.Print "Caught f_"
Case InStr(2, sString, "r_") > 0
Debug.Print "Caught r_"
Case Else
Debug.Print "Unknown"
End Select

End Sub
 
How about ---
Dim sString As String
Dim FirstLetter As String
sString = "f_test1"
FirstLetter = Left(sString,1)
If FirstLetter = "f" Then
MsgBox "Caught f"
ElseIf FirstLetter = "r" Then
MsgBox "Caught r"
Else
MsgBox "Unknown"
End If

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
The InStr function finds if the test string is anywhere in the string and
the first parameter, in your case 2, tells where to start looking. So it is
not what you need.

Change your code to:

Dim sString As String
Dim tmpstring As String

sString = "f_test1"

tmpstring = Left(sString, 2)
Select Case tmpstring

Case "f_"
Debug.Print "Caught f_"
Case "r_"
Debug.Print "Caught r_"
Case Else
Debug.Print "Unknown"
End Select


John... Visio MVP
 
How about actually reading the question? The OP was looking for the first
TWO characters.

John... Visio MVP
 
works great!

John Marshall said:
The InStr function finds if the test string is anywhere in the string and
the first parameter, in your case 2, tells where to start looking. So it
is not what you need.

Change your code to:

Dim sString As String
Dim tmpstring As String

sString = "f_test1"

tmpstring = Left(sString, 2)
Select Case tmpstring

Case "f_"
Debug.Print "Caught f_"
Case "r_"
Debug.Print "Caught r_"
Case Else
Debug.Print "Unknown"
End Select


John... Visio MVP
 
Steve said:
How about ---
Dim sString As String
Dim FirstLetter As String
sString = "f_test1"
FirstLetter = Left(sString,1)
If FirstLetter = "f" Then
MsgBox "Caught f"
ElseIf FirstLetter = "r" Then
MsgBox "Caught r"
Else
MsgBox "Unknown"
End If

PC Datasheet
Providing Customers Wromg Answers For Help With Access, Excel And Word
Applications

--
You are *not* a resource at all !!
Tell us you will stop advertising here, or get lost for another year or so...
http://home.tiscali.nl/arracom/whoissteve.html

ArnoR
 
Read the question, f____r , the Op is looking to see if "f_test1" begins
with an "f" or "r" just like the code you suggested.
 
Cute, you were able to use the three characters in a word. Since you have a
severe inability to read or understand, the OP said:
I'm trying to test the first 2 characters in a string and determine if the
string begins with "f_" or "r_".

The underscore at the end is a character and is part of the required test.
Otherwise words like "failure", "ridiculous" would pass the test.

If this is your level of accuracy, then you are definitely over charging,
especially for your "FREE" service. So isn't it time for you to dissappear
for the rest of the year?

John... Visio MVP

For the casual lurker, steve only attempts to answer questions in these
newsgroups so he can con unsuspecting users into paying for his questionalbe
services. He claims he has thousands of satisifed customers, but none have
ever come to his defense.
 
Steve said:
Read the question, f____r , the Op is looking to see if "f_test1" begins
with an "f" or "r" just like the code you suggested.

Starting to get angry Steve ...??
Just read more carefully I would suggest, no reason to get upset...

BTW: 149 pageloads only the last three days

ArnoR
 
Back
Top