By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,737 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How To Highlight Searched For Text

Merlin1857
P: 14
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 :

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. 'Highlight searched for text function
  4. Function Highlight(strText, strFind, strBefore, strAfter)
  5. Dim nPos
  6. Dim nLen
  7. Dim nLenAll
  8.  
  9. nLen = Len(strFind)
  10. nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1
  11.  
  12. Highlight = strText 
  13.  
  14. If nLen > 0 And Len(Highlight) > 0 Then
  15. nPos = InStr(1, Highlight, strFind, 1)
  16. Do While nPos > 0
  17. Highlight = Left(Highlight, nPos - 1) & _
  18.     strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
  19.     Mid(Highlight, nPos + nLen)
  20.  
  21. nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
  22. Loop
  23. End If
  24. End Function
  25.  
  26.  

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.


Expand|Select|Wrap|Line Numbers
  1. <%=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.
Sep 16 '07 #1
Share this Article
Share on Google+