A thing I have been asked for on a number of occasions is the ability to highlight text after its been searched for in a record return page. The following does this perfectly.
Use this function within your record return page :
-
-
-
'Highlight searched for text function
-
Function Highlight(strText, strFind, strBefore, strAfter)
-
Dim nPos
-
Dim nLen
-
Dim nLenAll
-
-
nLen = Len(strFind)
-
nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1
-
-
Highlight = strText
-
-
If nLen > 0 And Len(Highlight) > 0 Then
-
nPos = InStr(1, Highlight, strFind, 1)
-
Do While nPos > 0
-
Highlight = Left(Highlight, nPos - 1) & _
-
strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
-
Mid(Highlight, nPos + nLen)
-
-
nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
-
Loop
-
End If
-
End Function
-
-
To implement it within the page use the following code around your recordset return, in this case the text would be highlighted in bold red with a yellow background. The MYDATARETURN is what the database is giving back to you, the THESTRINGSEARCHEDFOR is a variable input which you will have used in your search.
- <%=Highlight(RS1("MYDATARETURN"),""&THESTRINGSEARCHEDFOR&"", "<b><font color=red><span style='background-color: yellow'>", "</span></font></b>")%>
It's really easy and very impressive. If you are using a multiple field search like those outlined in other articles I have posted wrapping this function around each of the returned data fields allows multiple highlighting in multiple fields, neat.