Results 1 to 4 of 4
  1. #1
    paxy is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2022
    Posts
    1

    Highlight Keyword Search Results

    Hi,



    I have a search box and I am trying to have the results highlighted.
    Example if user enters "photons quantum energy" in the search box, rows with the words "photons","quantum" or "energy" in the "ProposalAbstract" column will be filtered and any of the 3 words will be highlighted.
    So far I have managed to write the code for the search filtering only. Can someone help me with the code to highlight the search results. Thank you very much. By the way I have just started learning Access with no IT background.

    ------
    Option Compare Database


    Private Sub Command13_Click()


    Dim Keywords As String
    Dim fieldToSearch As String
    Dim Criteria As String
    Dim SQL As String


    ' remove leading and trailing spaces, if any, from user input.
    Keywords = Trim(Me.Find)


    ' specify which field to search.
    fieldToSearch = "[ProposalAbstract]"


    ' create criteria based on input.
    Do While InStr(Keywords, " ") > 0
    Criteria = Criteria & fieldToSearch & " Like ""*" & Left(Keywords, InStr(Keywords, " ") - 1) & "*"" or "
    Keywords = Mid(Keywords, InStr(Keywords, " ") + 1)
    Loop
    Criteria = Criteria & fieldToSearch & " Like ""*" & Keywords & "*"""
    Me.Filter = Criteria
    Me.FilterOn = True
    Me.Requery


    'Me.Find.SetFocus
    'Me.FilterOn = False
    'Me.Filter = "[ProposalAbstract] Like '*" & Find & "*'"
    'Me.FilterOn = True
    'Me.Requery


    End Sub


    Private Sub Command14_Click()
    DoCmd.ShowAllRecords
    Me.Find.SetFocus
    Me.Find = ""


    End Sub


    Private Sub Form_Load()
    Me.Find.SetFocus


    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    My initial thoughts were that this was a crosspost, but now believe it is not?
    Anyway have a look at what is offered to the same question here https://www.access-programmers.co.uk...esults.323303/
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    when posting more than a couple of lines of code, please use the code button (the # button) to preserve indentation. simply select the code then click the button.

    If by highlighting you mean your filter returns a phrase containing one or more of the keywords and you want something like

    'there are 355 photons in the example'

    you will need to use richtext and build your string to incorporate the relevant html coding. To see what that coding looks like, download the example on this link and experiment with various configurations
    https://www.access-programmers.co.uk.../#post-1528802

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Highlight Search results better
    By olivetwist in forum Access
    Replies: 1
    Last Post: 06-08-2017, 04:31 PM
  2. Replies: 8
    Last Post: 06-23-2016, 07:56 AM
  3. Highlight Duplicate Values in Query Results
    By TJ1010F in forum Queries
    Replies: 3
    Last Post: 06-21-2015, 05:51 PM
  4. how to highlight search keywords in results form?
    By Absolute_Beginner in forum Forms
    Replies: 2
    Last Post: 08-22-2011, 04:52 AM
  5. Replies: 0
    Last Post: 10-16-2008, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums