Home > Back-end >  Access VBA: control for editing html code?
Access VBA: control for editing html code?

Time:10-24

I have a Access table with records containg a lot of html code pieces. I want to create a form control to edit a retrieved piece of html codes. I know the Text Box control can be used for the job.

But I hope the Text Box control would color the html tags as in VSCode to make the editing job less painful. No control seems to be available for this.

What can I do to creat such a primitive IDE box in Access forms? Thank you!

CodePudding user response:

I made a little HTML syntax highlighter for Microsoft Access TextBoxes with the Text Format property set to Rich Text (it effectively uses HTML tags to format the text despite the name Rich Text). It is also advisable to set the Enter Key Behavior to New Line in Field for a better editing experience.

My code uses a RegExp object from the Microsoft VBScript Regular Expressions 5.5 DLL. Therefore you must set a reference to it in the VBA Editor.

Create a text Changed handler for your TextBox. My TextBox is named Text1, therefore the Sub is named Text1_Change. Replace it by <your textbox name>_Change:

Private Sub Text1_Change()
    Dim r As New RegExp
    Dim colMatches As MatchCollection
    Dim objMatch As Match
    Dim oldPos As Long, oldLen As Long, i As Long, pos As Long, l2 As Long
    Dim s As String, s1 As String, s2 As String, s3 As String, special As String
    Dim specialOffset As Long
    
    s = Replace(PlainText(Text1.Text), " ", "&nbsp;")
    With r
        .Pattern = "<[?!]?[^>/] /?>|</\w >"
        .IgnoreCase = True
        .Global = True
        .Multiline = False
        Set colMatches = .Execute(s)
    End With
    oldPos = Text1.SelStart
    oldLen = Text1.SelLength
    For i = colMatches.Count - 1 To 0 Step -1
        Set objMatch = colMatches(i)
        special = Mid$(objMatch.Value, 2, 1)
        specialOffset = IIf(special = "/" Or special = "?" Or special = "!", 1, 0)
        If specialOffset = 0 Then
            special = ""
        End If
        s1 = Left$(s, objMatch.FirstIndex) & "<font color=blue>&lt;" & special & "</font><font color=brown>"
        s2 = EscapeHtml(Mid$(objMatch.Value, 2   specialOffset))
        l2 = IIf(Mid$(s2, Len(s2) - 4, 1) = "/", 5, 4)
        pos = InStr(s2, "&nbsp;")
        If pos > 0 Then
            s2 = Left$(s2, pos   5) & "</font><font color=red>" & Mid$(s2, pos   6, Len(s2) - pos - l2 - 5) & "</font><font color=blue>" & Right$(s2, l2)
        Else
            s2 = Left$(s2, Len(s2) - l2) & "</font><font color=blue>" & Right$(s2, l2)
        End If
        s3 = "</font>" & Mid$(s, objMatch.FirstIndex   objMatch.Length   1)
        s = s1 & s2 & s3
    Next
    s = Replace(s, vbCrLf, "<br />")
    Text1.Text = s
    Text1.SelStart = oldPos
    Text1.SelLength = oldLen
End Sub

Private Function EscapeHtml(ByVal s As String) As String
    EscapeHtml = Replace(Replace(s, "<", "&lt;"), ">", "&gt;")
End Function

Don't forget the helper Function EscapeHtml.

The result looks like this:

enter image description here

Not perfect, but you are welcome to improve it.

You can retrieve the plain, unformatted text like this (PlainText is a built in function)

s = PlainText(Text1.Text)

CodePudding user response:

I made a form with a webbrowser control and a big textbox next to eachother bound to a table with the html in a textfield. The form loads the html text in the text field, where you can edit it and load it in the browser to show the result. You can also set EditOn = true and you can change the html in the browser itself, you have to write code if you want to save the changes made in the browser.

  • Related