Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you format different parts of a text box 1

Status
Not open for further replies.

NeilT123

Technical User
Jan 6, 2005
302
GB
I have Acccess 2007 and a text box on a report with the code

Code:
=([ProductName] & " @ " & [ApplicationRate] & [ApplicationUnits])

Is it possible to have [Productname] as one colour, font size and bold and [ApplicationRate] & [ApplicationUnits] as a different colour font and non bold, without having separate text boxes?
 
Yes, you must first go to the text box property and change the text type from plain text to rich text. Then when you concatenate your string you have to also add in HTML tags. To learn the tags I cheat. I build a simple unbound text box on a form and set it to rich text. Then I format the string using the formatting tools. I have a button on the form that then has
Debug.print txtbox.value
The tags will have some double quotes that you will have to replace with single quotes.
 

MajP said:
[highlight #FCE94F]I cheat[/highlight]. I build a simple unbound text box on a form and set it to rich text. Then I format the string using the formatting tools. I have a button on the form that then has
Debug.print txtbox.value
The tags will have some double quotes that you will have to replace with single quotes.

Wow! Some Major cheating going on here! Well I sure hope that this germinates into a Colonel of truth that flowers into General reality.[highlight #5C3566] Nice Tip![/highlight]

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
When I get time I will make a faq

Create a very simple form "RichTextHelper"
Add large input text box (rich text) and a large output text box (plain text). Add a command button to get the codes "cmdGetCodes".
here is the code

Code:
Private Sub Form_Load()
  'You can do this in design view, but in case you forget
  Me.txtBxInput.TextFormat = acTextFormatHTMLRichText
  Me.txtBxOutput.TextFormat = acTextFormatPlain
End Sub
Private Sub cmdGetCodes_Click()
  Me.txtBxOutput.Value = Me.txtBxInput.Value
  'Since you need to convert the single quotes most of the time to use
  Me.txtBxOutput.Value = Replace(Me.txtBxOutput.Value, """", "'")
End Sub

so in the input text box I typed this string
Bold Text and Red Text
I bolded the "Bold Text" and made the "Red Text" font red.
I click the button and in the output box I get the string

<div><strong>Bold Text</strong> and <font color=#ED1C24>Red Text</font></div>

To make this easier to understand here it is with some carriange return
<div>
<strong>
Bold Text
</strong> and
<font color=#ED1C24>
Red Text
</font>
</div>

notice that "bold text" is wrapped in the strong tag, and "red text" is wrapped in the font color tag. "and" is outside of any tags.

So to go in reverse, here is an example of code.
Add another command button "cmdTest"
Code:
Private Sub cmdTest_Click()
 UseTags
End Sub

Public Sub UseTags()
  Dim strOne As String
  Dim strTwo As String
  strOne = "Bold Text"
  strTwo = "Red Text"
  Me.txtBxInput.Value = "<strong>" & strOne & "</strong>" & " and "
  Me.txtBxInput.Value = Me.txtBxInput.Value & "<font color=#ED1C24>" & strTwo & "</font>"
  'need to surround with div
  Me.txtBxInput.Value = "<div>" & Me.txtBxInput.Value & "</div>"
  Me.txtBxOutput.Value = Me.txtBxInput.Value
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top