Formatting Keyword in Text or Memo field. (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Today, 13:12
Joined
Jan 10, 2011
Messages
904
Is there any way of conditional formatting a Keyword in a Text or Memo field? I tried to do a search on this but came up with nothing.

What I am trying to do is in a report, if I specifically set a temporary variable to a keyword and the keyword appears in a report's Text or Memo field, it will be highlighted.

This is much the same as when I search for something in this forum and the keyword appears in red in the results.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:12
Joined
Feb 28, 2001
Messages
27,128
In theory you can make text fields RTF, which allows for formatting of selected elements. I've never done it but you could search this forum to find articles on it.
 

Eljefegeneo

Still trying to learn
Local time
Today, 13:12
Joined
Jan 10, 2011
Messages
904
Appreciate all the replies. Allen Brown's example does not seem to have memo files, only text files. Nor so they seem to be RTF format. Am I mistaken?
 

GinaWhipp

AWF VIP
Local time
Today, 16:12
Joined
Jun 21, 2011
Messages
5,900
You are correct. As he states, further development would be needed as the *tags* used most likely will clash.
 

JHB

Have been here a while
Local time
Today, 22:12
Joined
Jun 17, 2012
Messages
7,732
.. Allen Brown's example does not seem to have memo files, only text files.
But you can do the same with a memo field, (replace). Remember to set the control in which you show the content of the memo field to RTF-format.
I would use a copy of the memo field content, else you've to set it back to what it was before you did the formatting.
 

isladogs

MVP / VIP
Local time
Today, 21:12
Joined
Jan 14, 2017
Messages
18,209
Apologies if I confused you with this example.
It works by replacing text with an unbound rich text box for display purposes only.
I have also successfully used it to highlight text from a memo field but the original was unformatted in my case.

As I said previously rich text can only be applied directly to memo fields
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:12
Joined
May 21, 2018
Messages
8,519
As stated the only field that can permanently hold Rich Text is a memo field. In the table design you need to set the Text Format property to RichText. The other choice is PlainText. However, as also stated unbound text boxes also have this property. The property is on the data tab.

The way Rich Text works is that it puts tags around your text. If you view this in a control set to Rich Text you will see the formatting. If you view the same thing in a plain text control you will see just the tags. This is the best way to learn how to manipulate RT. Build a form based on a rich text memo field. On the form put this field twice. Set one text box to rich text and the other to plain text. Now you can type in the RT and see the proper tags in the plain text.

Bold the word Bold and in the other textbox you would see
<div><strong>Bold</strong></div>

Underline the word underline
<div><u>underline</u> </div>

Highlight the word Yellow Highlight in yellow
<div><font style="BACKGROUND-COLOR:#FFFF00">Yellow Highlight</font></div>

Make the word yellow have yellow font
<div><font color="#FFF200">yellow</font> </div>

So if i want to bold the word "bold" in the below sentence, I do a replace

This word is Bold
to
This word is <strong>Bold</strong>

So if you want you can make a library of format that you would like to use. The function allows you to pass in a string and search for a word in the string and format it.

Code:
Public Enum RT_FormatType
  RT_Bold = 1
  RT_Underline = 2
  RT_Italic = 3
  RT_Red = 4
  RT_Blue = 5
  RT_BoldUnderline = 6
  RT_YellowHighlight = 7
End Enum

Public Function FormatWord(searchIn As String, searchFor As String, FormatType As RT_FormatType)
  Dim replaceText As String
  Select Case FormatType
  Case RT_Bold
        replaceText = "<strong>" & searchFor & "</strong>"
     Case RT_Underline
        replaceText = "<u>" & searchFor & "</u>"
     Case RT_Italic
        replaceText = "<em>" & searchFor & "</em>"
     Case RT_Red
        replaceText = "<font color=red>" & searchFor & "</font>"
     Case RT_Blue
        replaceText = "<font color=Blue>" & searchFor & "</font>"
     Case RT_BoldUnderline
        replaceText = "<strong>" & searchFor & "</strong>"
        replaceText = "<u>" & replaceText & "</u>"
     Case RT_YellowHighlight
        replaceText = "<font style= 'BACKGROUND-COLOR:#FFFF00'>" & searchFor & "</font>"
     Case Else
        replaceText = searchFor
   End Select
  searchIn = Replace(searchIn, searchFor, replaceText)
  FormatWord = searchIn
End Function

I would recommend you do it this way where you put the results into an unbound textbox instead of persisting this into the actual RT field. If you do it directly on the RT field then those formats will persist. If you want to un format the found words you would need to write code to replace the tagged word with no tags.
 

Eljefegeneo

Still trying to learn
Local time
Today, 13:12
Joined
Jan 10, 2011
Messages
904
Thanks. Possibly a little over my head, but I am game to try anything.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:12
Joined
May 21, 2018
Messages
8,519
it is basically the same as the allen browne post, just expanded the concept. Bottom line you replace your text with your text surrounded by the tags.
To bold "My Word" you would replace "My Word" with
<strong>My Word</strong>
If you view above in a rich text formatted control you would see
My Word
If you viewed it in a plain text control you would see
<strong>My Word</strong>
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:12
Joined
May 21, 2018
Messages
8,519
Oh, but going back to your original post. Assume you have a search word in tempvars and you want to turn it red in multiple fields then You should be able to build a query for the report and format all the fields in consideration.

Code:
Select FormatWord([someField],[Tempvars]![varname] ,4) as SomeFieldFormatted, FormatWord([SomeOtherField],[Tempvars]![varname] ,4) as someOtherFieldFormatted.....

But bottom line, setting a search word red in multiple fields in a report is definitely doable.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 21:12
Joined
Jan 14, 2017
Messages
18,209
Here is an example where I've used red bold text & highlighting for specified search words in a form & a report





However I don't use tempvars for this... or indeed ever!
 

Attachments

  • SQLSearchReport.gif
    SQLSearchReport.gif
    53.6 KB · Views: 505
  • SQLSearchForm.jpg
    SQLSearchForm.jpg
    44.3 KB · Views: 524

Users who are viewing this thread

Top Bottom