[AccessD] Rich text text boxes

John Colby jwcolby at gmail.com
Tue Jul 12 09:31:09 CDT 2022


I'm sure some of our more experienced members will slap me around, but I
just had to share my solution.

Problem: the rich text text box in access "only works" bound to a rich text
field.  Furthermore, the data in the field is HTML (ish) and all of that
formatting stuff apparently is not available to get back from said field.
Which makes writing rich text programmatically a roya PITA.  I have spent
the morning googling and found pretty much nothing, at least in the Access
vba side of the house.

Solution:  Wrote my own.

The answer lies in generating HTML dynamically.

However before you can do ANYTHING you have to get a text box with it's
format set to 'Rich Text' which is non-trivial.  In fact the only way I
have found to do so is to create a table with rich text fields, then bind a
form to that, then bind a text box to one of those fields, then set the
format of the text box to 'Rich text' which is suddenly available simply
because it is bound to a rich text field.  Once I had that , I unbound (is
that a word) the text box, but it kept the 'Rich Text' format.  Voila a
text box that can understand and process HTML tags.

>From there, I created a class (of course).  The objective of the class is
to wrap a 'Rich text' text box and allow vba to do the mucking around with
html format codes.

I pass in the text box to the init and store the text box in the header of
the class so I can access it inside of the class.

I then wrote a function to pass in a message I wanted to display in the
text box and a boolean to bold it or not, defaulted to not.

Function fWriteTextBox(lstrMsg As String, _
                            Optional blnBold As Boolean = False)

Dim lstr As String
    lstr = lstrMsg
    If blnBold Then
        lstr = "<b>" & lstr & "</b>"
    Else
    End If
    If mLineCnt > 0 Then
        lstr = "<br>" & lstr
    End If
    mstrHTMLMsg = mstrHTMLMsg & lstr
    mtxtStatus.SetFocus
  mtxtStatus.Text = mstrHTMLMsg
    mLineCnt = mLineCnt + 1

Exit_fWriteTextBox:
    On Error GoTo 0
    Exit Function
end function

In the form header

Dim mclsLicenseStatusText As clsStatusText

In the Form_Open

    Set mclsLicenseStatusText = New clsStatusText  'Instantiate the beast
    mclsLicenseStatusText.Init Me, txtLicenseStatus  'Pass in the text box

    mclsLicenseStatusText.fWriteTextBox "This is a test" 'Write a string
without bold
    mclsLicenseStatusText.fWriteTextBox "This is a BOLD test", True ' Again
with bold

GUESS WHAT!  IT WORKS!!!

The text box in my form displays the two lines of text stuff, one bolded,
the other not.

Can you say ROYAL PITA?  But IT WORKS.

So now I need to flesh out the class to allow other format tags.  I desire
to allow colors, bold, italics, font etc.  Slowly over time I think I can
do this thing.  We shall see if and what the limitations of the text box
are.  But I am simply jazzed that I can do anything at all of this nature.

Furthermore the built up HTML string is available for me to look at / read
back as opposed to the 'bound to a table' method.

OK, now the real heavyweights of the list can commence the slapping.🤕😜😁
-- 
John W. Colby
Colby Consulting


More information about the AccessD mailing list