Change color of substring in rich text field (1 Viewer)

Magster

Registered User.
Local time
Today, 06:32
Joined
Jul 30, 2008
Messages
115
Hello all,

I'm using Access 2010. I'm passing a string into the OpenArgs of my report - works fine. In the report there are 3 rich text fields which may contain the text I passed in, and if so I want to change the color of that text to red so it stands out.

The value passed to the report changes so I'll need to use VBA in the detail's format section to check each of the 3 rich text fields.

Can anyone point me in the right direction on this?

Thanks so much!
 

TJPoorman

Registered User.
Local time
Today, 07:32
Joined
Jul 23, 2013
Messages
402
This should work for you. You will need to change the field name highlighted in red:

Code:
Dim strTemp, strTempEnd As String
Dim strSearch As String
 
strSearch = Me.OpenArgs
 
If InStr(1, [COLOR=red]Me.Text0[/COLOR], strSearch) <> 0 Then
    strTemp = Left([COLOR=red]Me.Text0[/COLOR], InStr(1, [COLOR=red]Me.Text0[/COLOR], strSearch) - 1)
    strTempEnd = Mid([COLOR=red]Me.Text0[/COLOR], Len(strTemp) + Len(strSearch) + 1)
    strTemp = strTemp & "<font color=red>" & strSearch & "</font>"
    strTemp = strTemp & strTempEnd
    
    Me.Text0 = strTemp
End If
 

Magster

Registered User.
Local time
Today, 06:32
Joined
Jul 30, 2008
Messages
115
Thank you for your reply... and I think I'm almost there but, I can't quite get this to work.

I put your code in detail format event of my report. The code runs until the last line of code: me.Text0 = strTemp ( I used my field name which is me.TxtContactHistory), I can see that red tags have been added to the text field, but Access won't allow the value to be assigned to the report. The following error occurs:

"You can't assign a value to this object"

Am I inserting the code in the correct part of my report?
 

Magster

Registered User.
Local time
Today, 06:32
Joined
Jul 30, 2008
Messages
115
It's working now!

I put the code into the form that I was using for my search and I made the color change there and when the report runs, the search text is red!

Since this search value changes and I can't keep the red text, I'll just make sure to set the field color back to black when I'm done with the search.

Many thanks - you've saved me a lot of time!
 

Magster

Registered User.
Local time
Today, 06:32
Joined
Jul 30, 2008
Messages
115
Hello again!

Now that I have my search text highlighted red in my rich text field - and it prints out red in my report which is what I wanted - I now have to change the text back to black after the user is done with that particular search. There may be several rows that match the search criteria so I have to have a way to change all rows.

I've tried to use a query and a clone recordset to update the field to all black text but I can't get it to work. The only thing I can think of to do is use a copy of the table and let them search and then delete it each time a search is done.

It would be great to use a query, but can that be done? If not, any suggestions?
 

Magster

Registered User.
Local time
Today, 06:32
Joined
Jul 30, 2008
Messages
115
I stumbled upon the answer today and I wanted to share it.

There is a PlainText function which can be used in a query which returns a string formatted as plain text! This will remove my red color.

Thanks again to PJPoorman!
 

Users who are viewing this thread

Top Bottom