character-in-string search function

jguscs

Registered User.
Local time
Today, 15:33
Joined
Jun 23, 2003
Messages
148
Is there a specialized function that returns a true/false-type value if a given character is found in a given string?

I've looked but have not found one, though this type of thing seems like it should be available (from what I've seen of VB so far).
 
Nope.

It's not that hard to make a function that returns a boolean value for what you are asking.

Code:
Function SearchString(ByVal intStart As Integer, ByVal strTest As String, ByVal strSearch As String) As Boolean

    If InStr(intStart, strTest, strSearch) Then
        SearchString = True
    End If

End Function
 
Last edited:
Or, even better:

Code:
Function SearchString(ByVal strTest As String, ByVal strSearch As String, Optional intStart As Integer) As Boolean

    If IsMissing(intStart) Then
        If InStr(1, strTest, strSearch) Then
            SearchString = True
        End If
    Else
        If InStr(intStart, strTest, strSearch) Then
            SearchString = True
        End If
    End If

End Function
 
Last edited:
There is another way.......

instr does return true and false for a string search

try (IT WORKS)

Code:
if instr(1,SomeString,SomeTestString,,,vbtextcompare) then

       'sometesthing is contained within something
end if

if not instr(1,SomeString,SomeTestString,,,vbtextcompare) then

       'sometesthing isnt contained within something
end if







I have had a lot of problems with Replace and Instr in VBA so much that i now recommed the following....



Always say what compare method to use.

ie...

Code:
Function SearchString(ByVal strTest As String, ByVal strSearch As String, Optional intStart As Integer) As Boolean

    If IsMissing(intStart) Then
        If InStr(1, strTest, strSearch, , , vbTextCompare) Then
            SearchString = True
        End If
    Else
        If InStr(intStart, strTest, , , vbTextCompare)Then
            SearchString = True
        End If
    End If

End Function

This should stop any problem you have with the function
 
Last edited:
y cant i edit my posts............................[edit] i can now [/edit]



just to make things clear instr returns

0 - string is not present
0> - location of string
 
Last edited:
Thanks, Shadez and Mile-O-Phile, for your assistance.
This is what I was looking for:

If InStr(1, Me.TxtRename, "!", vbTextCompare) Or _
InStr(1, Me.TxtRename, ".", vbTextCompare) Or _
InStr(1, Me.TxtRename, "`", vbTextCompare) Then
MsgBox "The characters:" & vbCrLf & "! . `" & vbCrLf & "are not allowed.", , "Cannot Rename"
Else

Does anyone know how to combine the three parts of the "OR" together into one InStr?
 
I don't have a way to combine all those expressions into one, but how about using this expression:

Me.TxtRename Like "*!*"
 
I've not heard much about the Like operator and it seems fishy to me.
How standard is its use; would you recommend it above the InStr function?
Anyway, I tried the code and it does not work for checking the bracket characters [ and ] for some reason.
The error is Invalid String Pattern (93, I think) or something along those lines.
 
Fishy? Not at all. It's one of Access most-often used logical operators just like the Or or operator. Like does a comparison between two strings. It's most often used in query criteria. If you wanted to find all strings that began with the letter "a", you'd use this as the criteria:
Like "a*"

In fact, with some more investigating, I was able to find the solution to the problem that you posed. Use an expression of this form:
Me.TxtRename Like "*[[!.'*&%]*"
Basically, list any characters you want to search for Like "*[in here]*" .

That includes the [ brakcet. The reason it didn't work for you was because the Like operator considers it a special character because it uses the right bracket symbol to enclose other symbols. But, as long as you enclose the [ bracket within two other brackets like this [[], you're fine.
 
Last edited:
Neat.
The only problem I've still got is that it doesn't let me search for the ] bracket. I assume because it's looking for the first ] bracket as a special character in the same manner as it was looking for the first [ bracket.

Works:
If Me.TxtRename Like "*[`!['.]*" Then

Doesn't work:
If Me.TxtRename Like "*[`![]'.]*" Then

Works:
If Me.TxtRename Like "*[`!['.]*" Or Me.TxtRename Like "*]*" Then
 

Users who are viewing this thread

Back
Top Bottom