Go Back   Access World Forums > Microsoft Access Reference > Code Repository

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
Old 03-15-2018, 03:44 AM   #1
Webbarr
Newly Registered User
 
Join Date: Mar 2018
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Webbarr is on a distinguished road
Add HTML tags to your rich text

I've made a basic function that adds some HTML tags to my rich text boxes based on what I want to display to the user, E.G. red / bold text for warnings etc...

Below is the main function & some examples of how I use it.

It's not very sophisticated but it's served me well so far & I wanted to share it. If anyone has any suggestions for improvements would love to hear back!

Code:
Option Explicit
Public Enum udtHTMLTags
    bwBold = 1
    bwItalic = 2
    bwUnderline = 4
    bwLineBreak = 8
End Enum
Public Function fnAddHTML(strInput As String, Optional HtmlTags As udtHTMLTags, Optional strColour = vbNullString) As String
    Dim strStart As String, strEnd As String

    If HtmlTags And bwBold Then
        strStart = "<b>"
        strEnd = "</b>"
    End If

    If HtmlTags And bwItalic Then
        strStart = strStart & "<i>"
        strEnd = strEnd & "</i>"
    End If

    If HtmlTags And bwUnderline Then
        strStart = strStart & "<u>"
        strEnd = strEnd & "</u>"
    End If

    If strColour <> vbNullString Then
        strStart = strStart & "<font color=" & Chr(34) & strColour & Chr(34) & ">"
        strEnd = strEnd & "</font>"
    End If

    If HtmlTags And bwLineBreak Then
        fnAddHTML = strStart & strInput & strEnd & "<br>"
    Else
        fnAddHTML = strStart & strInput & strEnd
    End If
End Function


Public Function fnRedText(strWarningMessage As String) As String
    fnRedText = fnAddHTML("Warning: ", , "FF0000") & strWarningMessage
End Function


Public Sub PrintText()
    Dim strPrintText As String

    strPrintText = fnAddHTML("Some bold text", bwBold)
    Debug.Print strPrintText

    strPrintText = fnAddHTML("Some bold & underlined", bwBold + bwUnderline)
    Debug.Print strPrintText

    strPrintText = fnAddHTML("All the options", bwBold + bwItalic + bwUnderline + bwLineBreak, "00FF00")
    Debug.Print strPrintText
End Sub

Webbarr is offline  
Old 06-09-2018, 11:36 AM   #2
isladogs
Part time moderator
 
isladogs's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 9,824
Thanks: 107
Thanked 2,660 Times in 2,434 Posts
isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all
Re: Add HTML tags to your rich text

Thanks for this post which I've only just seen almost three months later.
You may be interested in an earlier post by cj_London on a similar theme
https://www.access-programmers.co.uk...d.php?t=290820
__________________
If this answer has helped, please click the Thanks button and/or click the 'reputation scales' symbol on the left.

Website links:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


Colin
Access 2010 32-bit, Access 2016 32-bit & 64-bit, SQL Server Express 2014, Windows 10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
isladogs is offline  
Old 06-12-2018, 02:34 AM   #3
Webbarr
Newly Registered User
 
Join Date: Mar 2018
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Webbarr is on a distinguished road
Re: Add HTML tags to your rich text

Thanks for posting that! I didn't receive a notification that I'd had a reply, maybe I need to have a look at my account settings.

It's an interesting read!

Webbarr is offline  
Old 07-23-2019, 08:47 AM   #4
FLJerseyBoy
Newly Registered User
 
Join Date: Jul 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
FLJerseyBoy is on a distinguished road
Re: Add HTML tags to your rich text

FWIW, I've done a little experimentation along these lines, if anyone is still following this thread.



When you concatenate multiple text fields to make one longer one which is rich text, you can specify their fonts by bracketing them with the deprecated HTML
Code:
font
element. So for instance to concatenate the string
Code:
red text
and the string
Code:
black text
-- and make the two substrings the indicated colors -- you could do something like this:


Code:
Me.ctlRichText = "<font color='#ED1C24'>red text</font>" & "<font color='#000000'>black text</font>"

The way Access handles this is to wrap each of the differently formatted font elements in
Code:
div
elements, which is a nuisance. So what you've got to do then is to strip out the
Code:
div
tags (opening and closing), using something like the
Code:
Replace()
function:


Code:
Me.ctlRichText = Replace(Replace(Me.ctlRichText, "<div>", ""), "</div>", "")

...which results in:
red textblack text
It's very awkward, but it can be done!
FLJerseyBoy is offline  
Old 07-27-2019, 04:31 AM   #5
isladogs
Part time moderator
 
isladogs's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 9,824
Thanks: 107
Thanked 2,660 Times in 2,434 Posts
isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all isladogs is a name known to all
Re: Add HTML tags to your rich text

The last post has just been spotted and approved. Posting to trigger email notifications.
Also see my Rich Text example at https://www.access-programmers.co.uk...78&postcount=2

A reminder to report your own posts to moderated areas. See sticky thread above.
__________________
If this answer has helped, please click the Thanks button and/or click the 'reputation scales' symbol on the left.

Website links:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


Colin
Access 2010 32-bit, Access 2016 32-bit & 64-bit, SQL Server Express 2014, Windows 10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by isladogs; 07-27-2019 at 04:43 AM.
isladogs is offline  
Old 07-27-2019, 05:57 AM   #6
CJ_London
Super Moderator
 
Join Date: Feb 2013
Location: UK
Posts: 11,025
Thanks: 40
Thanked 3,578 Times in 3,456 Posts
CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light
Re: Add HTML tags to your rich text

for those who want to find out the different formatting code and how they should appear in the string, this link might be of help

https://www.access-programmers.co.uk...d.php?t=290820
__________________
CJ_London
_______________________
A little thanks goes a long way. If you have found this post useful, please tick the thanks button
CJ_London is offline  
The Following User Says Thank You to CJ_London For This Useful Post:
MickJav (07-27-2019)
Old 07-27-2019, 12:57 PM   #7
MickJav
Newly Registered User
 
Join Date: Nov 2005
Location: Margate
Posts: 850
Thanks: 54
Thanked 40 Times in 34 Posts
MickJav will become famous soon enough
Re: Add HTML tags to your rich text

I've only just started working with rich text fields in both the current app I'm working on in the estimates and my project system I updated that as found them very usefull


thanks for the info


mick


__________________
All open code examples and free projects are only available from:

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

MickJav is online now  
Closed Thread

Tags
function , vba

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML tags displayed in table teel73 Tables 1 01-19-2015 10:17 AM
HTML in Rich text field adds ">" Zakraket Modules & VBA 4 10-21-2014 10:45 AM
insert rich text (html format) from acces to word 2013 nikolaus.hofer Modules & VBA 1 07-24-2014 08:13 AM
Remove HTML and RTF Tags from String GohDiamond Queries 3 12-06-2013 03:00 PM
Question Use variable between HTML tags byTimber General 3 09-30-2012 09:47 AM




All times are GMT -8. The time now is 03:57 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World