Connecting Tech Pros Worldwide Help | Site Map

How To Highlight Searched For Text

  #1  
Old September 16th, 2007, 09:02 AM
Merlin1857's Avatar
Newbie
 
Join Date: Sep 2007
Location: Nottingham
Posts: 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.



Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent | Highlight Row in DataView tirath answers 3 March 7th, 2007 03:15 PM
Highlight search text in datagrid Savvas answers 0 November 15th, 2006 09:55 AM
highlight a datagrid template row? Chris answers 4 November 19th, 2005 06:26 AM
Highlight Search Word in Results David Morgan answers 13 July 19th, 2005 02:56 PM