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.
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.
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.
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
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.
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>
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.