Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you serch a sting? 3

Status
Not open for further replies.

Trowser

IS-IT--Management
Dec 16, 2002
125
GB
I have info in a DB that then gets put into textboxes how do I search the sting in the textbox for a cetain word

IE
Nuts & bolts & string & carpet
Bolts & Sting & Carpet
Nuts & Carpet
Nuts & String & Carpet

how do I search those stings to see if the word carpet is contained in it
 
Look into the InStr function. Thanks and Good Luck!

zemp
 
Private Sub Command1_Click()
Dim t As Variant
Dim i As Integer

yourString = "Nuts & bolts & string & carpet &" + _
"Bolts & Sting & Carpet &" + _
"Nuts & Carpet &" + _
"Nuts & String & Carpet"
t = Split(yourString, "&")

For i = 0 To UBound(t)
If "carpet" = Trim(t(i)) Then
'it is equal
Else
'it is not equal
End If

Next
End Sub


Viola
 
Or, depending on what you need to do with the results, just use:

Dim bIsInStr As Boolean
bIsInStr = "Bolts & Sting & Carpet" Like "*Carpet*" [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
ok cool all really helpful suggestions :) great thanks
 
And to keep strongm happy.... the obligatory regexp solution...
Code:
Private Sub Command1_Click()
Dim objReg As RegExp
Dim sToSearch As String
Dim sToSearchFor As String

Dim colMatches As MatchCollection
Dim m As Match
sToSearch = "Nuts & bolts & string & carpet"

sToSearchFor = "carpet"

Set objReg = New RegExp
With objReg
    .Pattern = sToSearchFor & "\b"
    .IgnoreCase = True
    .Global = True
    Set colMatches = .Execute(sToSearch)
End With
If colMatches.Count > 0 Then
    For Each m In colMatches
        MsgBox m.Value & " was found at position " & CStr(m.FirstIndex) & " in string"
    Next
Else
    MsgBox "Not found"
End If
Set m = Nothing
Set colMatches = Nothing
Set objReg = Nothing
End Sub

take care all

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top