How do I make a search text box? like navigation buttons Search (1 Viewer)

Mario R. Perez

New member
Local time
Today, 04:38
Joined
May 14, 2020
Messages
16
Any ideas on how to make a search text box that will illuminate any text in a set of records in a form?

Attached a sample pic.

thanks for advance.
 

Attachments

  • 09-22-2024.png
    09-22-2024.png
    21.7 KB · Views: 45
it will not be highlighted unless Make column (field) is a Long text (memo) and displayed as Rich Text on your form.
here is a demo, open form Makes.
 

Attachments

Last edited:
It can be very simple with the right combination. This combination is very specific, but it gets the job done. It basically requires adding an unbound textbox with rich formatting. I added the unbound textbox to avoid changing my Short Text fields to Long Text, which is a requirement if you want to have a Rich Text textbox (or so I believe after my tests).

If you just want to highlight:
Code:
Private Sub txtSearch_Change()
    ' get term
    Dim x As String
    x = Me.txtSearch.Text
  
    ' show results
    If Len(x) > 0 Then
        Me.txtResults.ControlSource = "=Replace([TheField], '" & x & "', '<font style=""BACKGROUND-COLOR:#ffff00"">" & x & "</font>')"
    Else
        Me.txtResults.ControlSource = ""
    End If
  
    ' stay in textbox
    Me.txtSearch.SetFocus
    Me.txtSearch.SelStart = Len(Me.txtSearch.Text) + 1
End Sub

If you want to filter and highlight:
Code:
Private Sub txtSearch_Change()
    ' get term
    Dim x As String
    x = Me.txtSearch.Text

    ' filter
    Me.Filter = "TheField Like '*" & x & "*'"
    Me.FilterOn = True
  
    ' show results
    If Len(x) > 0 Then
        Me.txtResults.ControlSource = "=Replace([TheField], '" & x & "', '<font style=""BACKGROUND-COLOR:#ffff00"">" & x & "</font>')"
    Else
        Me.txtResults.ControlSource = ""
        Me.FilterOn = False
    End If
  
    ' stay in textbox
    Me.txtSearch.SetFocus
    Me.txtSearch.SelStart = Len(Me.txtSearch.Text) + 1
End Sub

Attached is a sample. This is the simplest setup I could come up with.
1727070389004.png
 

Attachments

Last edited:
a Rich Text textbox (or so I believe after my tests).
only problem is that if you have Capital letter on the original field and you typed on the search box lower case, the text on the unbound textbox got replaced with lowercase. same is true if you type Uppercase and the text on the field is lowercase, again, the unbound textbox got replaced with Uppercase?
 
Yes. I guess it's better if you find the position of the search term and inject the html tags before and after, but I hope this is simple enough for others to enhance it according to their needs.
 
a temporary table may do and prevent altering the original records.
 

Users who are viewing this thread

Back
Top Bottom