Results 1 to 4 of 4

Thread: Search capabillity

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Huntsville AL
    Posts
    2

    Search capabillity

    Here is my problem:

    I am trying to provide the ability to perform a search of records. The basic setup is as follows:

    MS Access 2003
    Visual Basic 6.3

    Form as single form : frmAcftEditor
    Subform as continuous form: subFaultViewer
    Search is initiated with cmdSearchFaults command button and is located on header of subFaultViewer

    Desired result is to enter search criteria and display all records containing criteria.

    Here is the code I am attempting, I have tried other variations as well with no success. I am fairly new to VBA and not well versed as of yet. Hope someone can help and thanks in advance!

    VB Code:
    1. Private Sub cmdSearchFaults_Click()
    2.  
    3. Dim strDescription As String
    4. Dim strCriteria As String
    5. Dim strFilter As String
    6. Dim strQuote As String
    7.  
    8. strQuote = Chr(34)
    9. ''' FaultDescription is a text field with 255 chr max length
    10. strDescription = Me.FaultDescription
    11. strCriteria = InputBox("Enter search word.")
    12. ''' search evaluates FaultDescription to see if it contains strCriteria
    13. ''' if so it returns the contents of fault description thus evaluating to true
    14. ''' if not it returns a zero lenth string "" evaluating to false
    15. ''' however it does not seem as though it evaluates the function for each record as the filter is applied
    16. strFilter = strQuote & "[FaultDescription] = " & "'" & Search(strDescription, strCriteria) & "'" & strQuote
    17.  
    18. Me.Filter = strFilter
    19. Me.FilterOn = True
    20.    
    21. End Sub
    22.  
    23.  
    24.  
    25. Public Function Search(Text, Word) As String
    26.  
    27. If InStr(1, Text, Word, 1) > 0 Then
    28.     Search = Text
    29. Else: Search = ""
    30. End If
    31.  
    32. End Function
    Last edited by si_the_geek; Feb 17th, 2006 at 10:45 AM. Reason: changed Code tags to VBCode

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search capabillity

    Welcome to VBForums!

    I could be wrong here (I don't use .Filter), but aren't the strQuote's unnescessary?

    Also, what is the purpose of comparing in the Search function? The Filter should be doing all the work for you.

    Try this instead:
    VB Code:
    1. Private Sub cmdSearchFaults_Click()
    2.  
    3. Dim strCriteria As String
    4. Dim strFilter As String
    5.  
    6. strCriteria = InputBox("Enter search word.")
    7. strFilter = "[FaultDescription] = '" & strCriteria & "'"
    8.  
    9. Me.Filter = strFilter
    10. Me.FilterOn = True
    11.  
    12. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Location
    Huntsville AL
    Posts
    2

    Re: Search capabillity

    Thanks. .Filter only looks for exact matches. I need to find if the field contains the search criteria not if it is a match to it. i.e. the field may contain several entries containing 5 to 20 words and I want to see only those records containing the word "cracked".

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search capabillity

    Your previous code method would unfortunately not work, as you were only changing the text to search for in the filter - the same work would still be done (or the text would be cleared, in which case only records with a blank FaultDescription would be returned).

    I don't know if Filter supports Like matching (partial text match), but if it does you can use one of these (probably the first):
    VB Code:
    1. strFilter = "[FaultDescription] Like '*" & strCriteria & "*'"
    2. strFilter = "[FaultDescription] Like '%" & strCriteria & "%'"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width