Results 1 to 7 of 7
  1. #1
    westfallbp is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2021
    Posts
    26

    Highlighting a word searched for in a form

    I have a form based on a query that returns records on a form: Like "*" & [Type anything you wish to search] & "*"



    Is it possible in MS Access that it highlights the word in the field to make it easier to locate. Some of the records returned in the field it searches can be several paragraphs long. I hope I communicated my question correctly. Would be like using find in ms word. Thanks, any help would be greatly appreciated.

  2. #2
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    Only Richtext format (Long Text) field can be highlighted.

  3. #3
    westfallbp is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2021
    Posts
    26
    It is set to Long Text in the table

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

  5. #5
    westfallbp is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2021
    Posts
    26
    Quote Originally Posted by orange View Post
    See this by Allen Browne.
    Thanks, It may work. I googled it also a few hours ago and watched a video on the find function but uses command button find first, find next, but it didn't search through all the records. Will get back to you if I can adapt Allen Browne's.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    There is a sample database on the linked page I recommended. Reviw the article and the associated database.

    Here is the code to find and highlight the desired text.
    I have modified code to bold and color the found text RED

    Code:
    Private Sub txtSearchText_AfterUpdate()
    On Error GoTo Err_Handler
        'Purpose:   Filter, and highlight matches in txtSearchDisplay.
        Dim strField As String              'The field to search
        Dim strSearchValue As String        'The value to find
        Dim strControlSource As String      'ControlSource for displaying match.
        Const strcWildcard = "*"            'Some other back ends use %
        'HTML tags for highlighting matches. Could be just "<b>" and "</b>".
        'Using Bold and RED
        Const strcTagStart = "<b><font  color=""""red"""">"  'jed Feb 2025<----modified line
        Const strcTagEnd = "</font></b>"
        
        'Save any edits.
        If Me.Dirty Then
            Me.Dirty = False
        End If
        
        'Apply a filter only if user chose a field and a value.
        If IsNull(Me.cboField) Or IsNull(Me.txtSearchText) Then
            If Me.FilterOn Then
                Me.FilterOn = False
            End If
            Call ShowHide(Me.txtSearchDisplay, False)
        Else
            strField = "[" & Me.cboField & "]"  'Cope with spaces in field name.
            strSearchValue = Me.txtSearchText
            'Apply the filter
            Me.Filter = strField & " Like """ & strcWildcard & strSearchValue & strcWildcard & """"
            Me.FilterOn = True
            
            'Control Source for the text box to display matches.
            strControlSource = "=IIf(" & strField & " Is Null, Null, " & _
                "Replace(" & strField & ", """ & strSearchValue & """, """ & _
                strcTagStart & strSearchValue & strcTagEnd & """))"
            With Me.txtSearchDisplay
                .ControlSource = strControlSource
                .Visible = True
            End With
        End If
    
    Exit_Handler:
        Exit Sub
    
    Err_Handler:
        Call LogError(Err.Number, Err.Description, conMod & "txtSearchText_AfterUpdate")
        Resume Exit_Handler
    End Sub

  7. #7
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    here is also a demo.
    open form, sampleForm.

    don't use Replace() function because this
    will change the Case of your text.
    If your search in small letters then if you have
    same text in your field but in Capital letter, the
    capital letter will be replaced with small letter.
    Attached Files Attached Files

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

Similar Threads

  1. Replies: 2
    Last Post: 10-13-2016, 08:54 AM
  2. Highlighting duplicate records in a form
    By zdjbel in forum Forms
    Replies: 9
    Last Post: 08-15-2016, 08:13 PM
  3. Highlighting required fields on a form
    By decco21 in forum Forms
    Replies: 6
    Last Post: 02-18-2015, 08:26 PM
  4. Replies: 4
    Last Post: 05-14-2013, 06:07 AM
  5. Replies: 2
    Last Post: 02-28-2013, 07:29 AM

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