How format text string as Rich Text in VBA code

Posted on 2014-07-28
Last Modified: 2014-07-30
Thanks to an expert last week the following code concatenates strings into one text field.  Is there a way to make it Rich Text?

Dim strFruits as string

  strFruits = ""

  If Me.chkBox1 = True then
     strFruits = strFruits "OR Apple"
 End If

  If Me.chkBox2 = True then
     strFruits = strFruits "OR Orange"
 End If

 ' rem and so on
 ' Then

 ' Strip off the first OR
 If strFruits<>"" then
   strFruits = Mid$(strFruits,4)
   strMessage = "The fruits available are " & strFruits & " for you"
   strMessage "No fruits for you"
 End If 

Open in new window

Question by:SteveL13
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Expert Comment

ID: 40225704
Rich text implies some format. Do you have any particular format in mind, i.e. make Apple bold and OR italic?
Or you just want to display it in a Rich Text control. If the latter than you do not need to do anything. Just assign it to the RichText control on your for and it will be displayed without any problems, like this:
Me!RichText1.Text = strFruits

Open in new window

LVL 13

Assisted Solution

by:Russell Fox
Russell Fox earned 166 total points
ID: 40225719
If you're going to display it in a rich textbox, you can use some html tags:
<div>			define some settings for a section of text
<em>			font-size in em's
<font>			define the font to use between <font></font>
<ol>			numbered list ("ordered")
	<li>		list item
<strong>		bold
<u>				underline
<ul>			bulleted list ("unordered")
	<li>		list item

Open in new window

LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 167 total points
ID: 40225868
In MS Access 2007 and higher "Rich Text" is really just HTML.

So you can format your string like this:

    ...strFruits will be BOLD

    ...strFruits will be UNDERLINED


But like the prior experts have stated, you don't just "Make" text Rich Text.

You simply add the HTML formatting tags and display the text in a textbox.
Be sure to use a textbox with its "TEXT FORMAT" property set to: Rich Text.

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

LVL 84
ID: 40226310
In MS Access 2007 and higher "Rich Text" is really just HTML.
"Rich Text" can be the standard .rtf format as well, but I agree with the others that you should use HTML when doing this. RTF is challenging, to say the least, to create. It's tag based, but the way the tags nest and interact with each other is insanely difficult, and it's quite easy to break the whole structure with a misplaced RTF tag. Unless you really need the high-level formatting of RTF (and if you do, you probably shouldn't be using Access for that) then just stick to HTML.

Author Comment

ID: 40226469
I didn't explain this well enough.  What I meant is that I want to be able to highlight part of the text that is in the concatenated block of text and bold just the highlighted portion, or underscore, or italics.  I can do this with a "normal" text field but can't seem to do so with a concatenated text field.
LVL 84

Accepted Solution

Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 167 total points
ID: 40226527
You can do this with any String value, so I'm not sure what you're asking. For example, using your code above:

 ' Strip off the first OR
 If strFruits<>"" then
   strFruits = Mid$(strFruits,4)
   strMessage = "The fruits available are <b>" & strFruits & "</b> for you"
   strMessage "<u>No fruits for you</u>"
 End If

The above changes would show in BOLD the text contained in your strFruits variable. It would also underline the "No Fruits for you" text.

Jeff has shown you how to make portions of your text Bold (i.e. use the <b></b> tags), and Russell has shown you several different HTML tags you can use. There are many HTML references around the internet that will show you all the tags available (although you should generally stick with the basics, like Bold or Underline).
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40227764

I just popped in here to lend a hand.
Scott and the prior experts have set you on the correct path...

No points for me...


Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linking Child/Parent fields between 2 subforms 15 61
Omit After Update event 5 47 dbnull syntax 1 59
Access Database 2007 - automating reports being sent by email 2 58
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question