Why won't this work?

'I would like to see 2 matches
'case1 and case4

Const Case1 = "ABC def"
Const Case2 = "abcd ef"
Const Case3 = "abc def"
Const Case4 = "def abc"

Private Sub Command1_Click()
Dim i As Integer
Dim x As String
x = "ABC def and deff and defab def abc deff abcd eg"
For i = 1 To 4
Select Case i
Case 1
If x Like Case1 Then MsgBox Case1 & " Found a match"
Case 2
If x Like Case2 Then MsgBox Case2 & " Found a match"
Case 3
If x Like Case3 Then MsgBox Case3 & " Found a match"
Case 4
If x Like Case4 Then MsgBox Case4 & " Found a match"
End Select
Next i
End Sub
LVL 4
JuiletteAsked:
Who is Participating?
 
MicrosoftConnect With a Mentor Commented:
your going about this the wrong way, your code to me looks like that your are attempting to detect when your type a case which has these conditions

ABC DEF
or
abcd ef
or
abc de

if this is the case your dont need to be using case.

you need to be using mid string

first of all am i write in what im saying


0
 
mcmahon_sCommented:
This looks like really weird code, what are you trying to do?
0
 
alaplumeCommented:
Use wildcards * and #
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
MicrosoftCommented:
have a look at this link, download the code this will show you how to use case select correctly.

http://www.freevbcode.com/ShowCode.Asp?ID=1027
0
 
MicrosoftCommented:
alaplume dont answer the question use comment, you have now locked the question for other users to comment.

this is normally classed as rude, you will get alot of stick from other EE users if you take this method.

I would decline his answer Juliette so that others can give you a solution to your question

he hasnt offered a correct answer, he has told you how to solve for problem and what he has suggested wouldn't work.

cheers
andy
0
 
MicrosoftCommented:
actually it probley makes sence for his stupid answer. he's never had an answer accepted.

lol

meow....
0
 
JuiletteAuthor Commented:
that is where I am having the problem...in the use of the wild cards..the Case select works..for example if I change my string to one of the cases it will message me with found.
What I can't get to work is the Like Function?

If you are going to give an answer then you should give an answer...I know how to use wild cards in some instances but in this particular one I'm lost.
0
 
JuiletteAuthor Commented:
the like function whoudl fine one occurance of your string within a string.
so if "abc" like "abc"  then I have a match
In simple terms I want to fine "abc" in "ghijf as abc cda"

That is my problem...I should not have to use mid since that is slower as it has to read each character...Like Function is what I want...I just can't figure it out.

Example:
If "1234" Like "*#*" Then MsgBox "like"
If "1234 abc" Like "*1234*" Then MsgBox "OK"
both of these work but when I use a variable, I don't know how to use the function.
0
 
JuiletteAuthor Commented:
Microsoft, I am giving you the points as it's not worth the time to try and retrieve them...at least you put out an effort.

For anyone who searches this code, the corrected code is here...I figured it out...dumb old concatenation...
Const Case1 = "ABC def"
Const Case2 = "abcd ef"
Const Case3 = "abc def"
Const Case4 = "def abc"

Private Sub Command1_Click()

Dim i As Integer
Dim x As String
x = "ABC def and deff and defab def abc and abcd eg"
For i = 1 To 4
Select Case i
Case 1
If x Like "*" & Case1 & "*" Then MsgBox Case1 & " Found a match"
Case 2
If x Like "*" & Case2 & "*" Then MsgBox Case2 & " Found a match"
Case 3
If x Like "*" & Case3 & "*" Then MsgBox Case3 & " Found a match"
Case 4
If x Like "*" & Case4 & "*" Then MsgBox Case4 & " Found a match"
End Select
Next i
End Sub
0
 
ArkCommented:
Hi
Why not to use Instr function instead of Like?

If InStr(1,x,Case1,vbBinaryCompare) Then MsgBox...

Cheers
0
 
JuiletteAuthor Commented:
Microsoft, I am giving you the points as it's not worth the time to try and retrieve them...at least you put out an effort.

For anyone who searches this code, the corrected code is here...I figured it out...dumb old concatenation...
Const Case1 = "ABC def"
Const Case2 = "abcd ef"
Const Case3 = "abc def"
Const Case4 = "def abc"

Private Sub Command1_Click()

Dim i As Integer
Dim x As String
x = "ABC def and deff and defab def abc and abcd eg"
For i = 1 To 4
Select Case i
Case 1
If x Like "*" & Case1 & "*" Then MsgBox Case1 & " Found a match"
Case 2
If x Like "*" & Case2 & "*" Then MsgBox Case2 & " Found a match"
Case 3
If x Like "*" & Case3 & "*" Then MsgBox Case3 & " Found a match"
Case 4
If x Like "*" & Case4 & "*" Then MsgBox Case4 & " Found a match"
End Select
Next i
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.