Finding if text is included

Guy Boswell

Registered User.
Local time
Today, 14:05
Joined
Jul 20, 2009
Messages
26
This should be so simple but I can't work it out.

I want to add a paragraph to piece of text if some other information isn't already in the text. That is to say if the long string doesn't include string1 and the long string doesn't include string2 then add string3 to the end of the long string.

I thought it would be something like

If LongString <> "%String1%" And LongString <> "%String2%" Then
LongString = LongString & String3
End If

But that tags on String3 everytime, even if LongString includes String1 or String2

Maybe % isn't the wild card, I tried * as well. Or maybe my statement is fundementally flawed. Am I makin this much more complicated than it needs to be?

Is there a function, 'Does_this_text_include_those_other_texts'?

Can any of you good people please help?



I am using MS Access 2003 SP2 and MS Outlook 2003 SP2 in Windows 2000 Professional SP4 in an Agency network where everything is locked down and I can't generally get at any settings!
 
This should be so simple but I can't work it out.

I want to add a paragraph to piece of text if some other information isn't already in the text. That is to say if the long string doesn't include string1 and the long string doesn't include string2 then add string3 to the end of the long string.

I thought it would be something like

If LongString <> "%String1%" And LongString <> "%String2%" Then
LongString = LongString & String3
End If

But that tags on String3 everytime, even if LongString includes String1 or String2

Maybe % isn't the wild card, I tried * as well. Or maybe my statement is fundementally flawed. Am I makin this much more complicated than it needs to be?

Is there a function, 'Does_this_text_include_those_other_texts'?

Can any of you good people please help?

I am using MS Access 2003 SP2 and MS Outlook 2003 SP2 in Windows 2000 Professional SP4 in an Agency network where everything is locked down and I can't generally get at any settings!

You do not want to do a comparison here. Instead you want to use the LIKE command. Something of this nature should be OK:
Code:
If ((LongString LIKE "%String1%") And (LongString LIKE "%String2%")) Then
    LongString = LongString & String3
End If

If this is SQL Code, you might need to use IIf():
Code:
IIf (((LongString NOT LIKE "%String1%") And (LongString NOT LIKE "%String2%")), LongString & String3, LongString)
 
It was the, 'not like', bit that was foxing me but MS Access Rookie put me on the right lines thank you. THe following is what I ended up with.

Option Compare Database

Dim LongString, String1, String2, String3 As String

Public Sub testlike()
LongString = "This message is ok!"
String1 = "bananas"
String2 = "apples"
String3 = "But there isn't any fruit"

If Not ((LongString Like "%String1%") And Not (LongString Like "%String2%")) Then
LongString = LongString & String3
End If
MsgBox LongString

End Sub

But that doesn't work either :-(
 
Last edited:
Ok! Got it now. The wild card I need is an asterisk *

Option Compare Database

Dim LongString, String1, String2, String3 As String


Public Sub testlike()
LongString = "This message is ok!"
String3 = "But there isn't any fruit"

If Not (LongString Like "*bananas*") And Not (LongString Like "*apples*")) Then
LongString = LongString & String3
End If
MsgBox LongString

End Sub
 

Users who are viewing this thread

Back
Top Bottom