VB: Check if a string contains a specific word

Posted on 2004-11-18
Last Modified: 2010-04-17
Using VB on an Access Table, I want to select some field if the field value which is a string of 3 or 4 words has the third word or the fourth word be the word "Spot".  How do I check this?

One way may be to find the second blank space character, take the Right of that to crop out that space and everything before that and then do a Like "Spot*"?

What would be good VB code for this?

Question by:bcsmess
    LVL 95

    Expert Comment

    by:Lee W, MVP
    Perhaps some combination using INSTR?

    Or perhaps SPLIT the string into a variable array and check the 3rd and 4th variables for that string?
    LVL 48

    Accepted Solution

    IF Instr(yourStr,"TheWord") > 0 then
      msgbox "Itsthere"
     msgbox "Not there"
    end if

    Expert Comment

    Will this help you any, Copy and paste, Call the Function IsSpotThere with the sentence to check
    It will return the value below, just check this value.


    Function IsSpotThere(strWord) As Integer

    'returns 0 if Spot is not in the 3rd or 4th Word
    'returns 1 if Spot is in the 3rd Word
    'returns 2 if Spot is in the 4rd Word
    'returns 3 if Spot is in the 3rd and 4th Word

    Dim Arr_Word

    ' Remember Split is Zero Based
    Arr_Word = Split(strWord, " ")

    IsSpotThere = 0

    If UBound(Arr_Word) > 1 Then If InStr(1, UCase(Arr_Word(2)), "SPOT") > 0 Then IsSpotThere = 1
    If UBound(Arr_Word) > 2 Then If InStr(1, UCase(Arr_Word(3)), "SPOT") > 0 Then IsSpotThere = IsSpotThere + 2
    End Function

    LVL 3

    Expert Comment

    Here is an example using Split

        Dim sText As String
        Dim sFind As String
        Dim iWord As Integer
        sText = "Now is the time for all good men to come to the aid of their country."
        sFind = "the"
        If MatchWord(sText, sFind, 4) Then
            MsgBox "found word"
        End If

    Function MatchWord(sText As String, sFind As String, iWord As Integer) As Integer
        Dim sWord() As String
        Dim iFound As Integer
        sWord = Split(sText, " ")
        iFound = False
        Select Case iWord - 1
            Case 0 To UBound(sWord)
                iFound = CBool(UCase$(Trim$(sWord(iWord - 1))) = UCase$(Trim$(sFind)))
        End Select
        MatchWord = iFound
    End Function


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    for loop with Set 4 38
    factorial example challenge 10 45
    word0 challenge 4 37
    groovy example issue 10 35
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 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