Solved

Why won't this work?

Posted on 2001-06-13
11
193 Views
Last Modified: 2010-05-02
'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
0
Comment
Question by:Juilette
11 Comments
 
LVL 4

Expert Comment

by:mcmahon_s
ID: 6188644
This looks like really weird code, what are you trying to do?
0
 
LVL 2

Accepted Solution

by:
Microsoft earned 50 total points
ID: 6188661
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
 
LVL 1

Expert Comment

by:alaplume
ID: 6188679
Use wildcards * and #
0
 
LVL 2

Expert Comment

by:Microsoft
ID: 6188700
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
 
LVL 2

Expert Comment

by:Microsoft
ID: 6188721
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:Microsoft
ID: 6188729
actually it probley makes sence for his stupid answer. he's never had an answer accepted.

lol

meow....
0
 
LVL 4

Author Comment

by:Juilette
ID: 6188976
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
 
LVL 4

Author Comment

by:Juilette
ID: 6188983
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
 
LVL 4

Author Comment

by:Juilette
ID: 6189023
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
 
LVL 27

Expert Comment

by:Ark
ID: 6189024
Hi
Why not to use Instr function instead of Like?

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

Cheers
0
 
LVL 4

Author Comment

by:Juilette
ID: 6189029
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now