Removing html tags from field

  • Thread starter Thread starter JA
  • Start date Start date
J

JA

First, thank you all who answered my previous questions. And now I have
another.

Is there a way to remove html tags from a memo field? Like a way to remove
the "<" and the ">" and anything in between them. And maybe replace them
with a space? There would most likely be more than one tag per record.

Thanks again!

JA
 
Public Function KillHTML(sText As String) As String
'Kills HTML tags
Dim iLeft As Integer
Dim iRight As Integer
Dim sTmp As String

iLeft = InStr(1, sText, "<")
While iLeft > 0
iRight = InStr(iLeft + 1, sText, ">")
sTmp = Mid(sText, iLeft, iRight - iLeft + 1)
sText = Mid(sText, 1, iLeft - 1) & Mid(sText, iLeft + Len(sTmp))
iLeft = InStr(1, sText, "<")
Wend

KillHTML = sText
End Function

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
I just remembered - you want to replace the tags with a space or something.

Public Function KillHTML(sText As String, sReplaceWith As String) As String
'Kills HTML tags
Dim iLeft As Integer
Dim iRight As Integer
Dim sTmp As String

iLeft = InStr(1, sText, "<")
While iLeft > 0
iRight = InStr(iLeft + 1, sText, ">")
sTmp = Mid(sText, iLeft, iRight - iLeft + 1)
sText = Mid(sText, 1, iLeft - 1) & sReplaceWith & Mid(sText, iLeft +
Len(sTmp))
iLeft = InStr(1, sText, "<")
Wend

KillHTML = sText
End Function

You can call this function from SQL, like so:
SELECT KillHTML([myField], " ") As NewText
FROM tblMyTable

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
Graham,

Thanks. I am just now getting to this. I don't quite know how to call it. I
put the first part in a module. Then I made an update query, and put

KillHTML([myField], " ")

as the thing to update it to, but that doesn't seem to work. I put the real
fieldname in for "myField". But I don't think I'm doing it right.

Thank you,

JA


Graham R Seach said:
I just remembered - you want to replace the tags with a space or something.

Public Function KillHTML(sText As String, sReplaceWith As String) As String
'Kills HTML tags
Dim iLeft As Integer
Dim iRight As Integer
Dim sTmp As String

iLeft = InStr(1, sText, "<")
While iLeft > 0
iRight = InStr(iLeft + 1, sText, ">")
sTmp = Mid(sText, iLeft, iRight - iLeft + 1)
sText = Mid(sText, 1, iLeft - 1) & sReplaceWith & Mid(sText, iLeft +
Len(sTmp))
iLeft = InStr(1, sText, "<")
Wend

KillHTML = sText
End Function

You can call this function from SQL, like so:
SELECT KillHTML([myField], " ") As NewText
FROM tblMyTable

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


JA said:
First, thank you all who answered my previous questions. And now I have
another.

Is there a way to remove html tags from a memo field? Like a way to remove
the "<" and the ">" and anything in between them. And maybe replace them
with a space? There would most likely be more than one tag per record.

Thanks again!

JA
 
You didn't happen to name the module KillHTML, did you? Modules can't have
the same name as functions or subroutines.

If that's not the problem, what happens when you run your query? Do you get
any error messages? If so, what are they?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



JA said:
Graham,

Thanks. I am just now getting to this. I don't quite know how to call it. I
put the first part in a module. Then I made an update query, and put

KillHTML([myField], " ")

as the thing to update it to, but that doesn't seem to work. I put the real
fieldname in for "myField". But I don't think I'm doing it right.

Thank you,

JA


Graham R Seach said:
I just remembered - you want to replace the tags with a space or something.

Public Function KillHTML(sText As String, sReplaceWith As String) As String
'Kills HTML tags
Dim iLeft As Integer
Dim iRight As Integer
Dim sTmp As String

iLeft = InStr(1, sText, "<")
While iLeft > 0
iRight = InStr(iLeft + 1, sText, ">")
sTmp = Mid(sText, iLeft, iRight - iLeft + 1)
sText = Mid(sText, 1, iLeft - 1) & sReplaceWith & Mid(sText,
iLeft
+
Len(sTmp))
iLeft = InStr(1, sText, "<")
Wend

KillHTML = sText
End Function

You can call this function from SQL, like so:
SELECT KillHTML([myField], " ") As NewText
FROM tblMyTable

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


JA said:
First, thank you all who answered my previous questions. And now I have
another.

Is there a way to remove html tags from a memo field? Like a way to remove
the "<" and the ">" and anything in between them. And maybe replace them
with a space? There would most likely be more than one tag per record.

Thanks again!

JA
 
As Doug says, the module and the function can't share the same name. Also,
when you examine the function in the IDE (code design window), is anything
shown in red? If so, the newsreader may have split a line or two. Show us
the code you have now.

As regards the update query, you should have something like this:
UPDATE tblMyTable SET myField = KillHTML([myField], " ")

And of course you renamed [tblMyTable] and [myField] to reflect the names of
your table and field ... right?

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


JA said:
Graham,

Thanks. I am just now getting to this. I don't quite know how to call it. I
put the first part in a module. Then I made an update query, and put

KillHTML([myField], " ")

as the thing to update it to, but that doesn't seem to work. I put the real
fieldname in for "myField". But I don't think I'm doing it right.

Thank you,

JA


Graham R Seach said:
I just remembered - you want to replace the tags with a space or something.

Public Function KillHTML(sText As String, sReplaceWith As String) As String
'Kills HTML tags
Dim iLeft As Integer
Dim iRight As Integer
Dim sTmp As String

iLeft = InStr(1, sText, "<")
While iLeft > 0
iRight = InStr(iLeft + 1, sText, ">")
sTmp = Mid(sText, iLeft, iRight - iLeft + 1)
sText = Mid(sText, 1, iLeft - 1) & sReplaceWith & Mid(sText,
iLeft
+
Len(sTmp))
iLeft = InStr(1, sText, "<")
Wend

KillHTML = sText
End Function

You can call this function from SQL, like so:
SELECT KillHTML([myField], " ") As NewText
FROM tblMyTable

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


JA said:
First, thank you all who answered my previous questions. And now I have
another.

Is there a way to remove html tags from a memo field? Like a way to remove
the "<" and the ">" and anything in between them. And maybe replace them
with a space? There would most likely be more than one tag per record.

Thanks again!

JA
 
Back
Top