473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,382 developers and data experts.

How To Highlight Searched For Text

Merlin1857
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
0 6802

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight =...
3
by: Christophe Guillon | last post by:
Hello I would like to have a hypertext link which, when I click on it, leads to a web page where a certain word is highlighted. Depending on the link, the same target page would be displayed but...
4
by: Chris | last post by:
I've searched, reviewed, and attempted dozens of code ideas on the subject. I can't seem to get something that seems so simple to work. To highlight a row in a datagrid based on another value in...
5
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be...
0
by: Savvas | last post by:
Hi, I am using VB.Net 2005 for a windows application. The application was actually upgraded from vb.net 2003. I have a search field which is used as a parameter to search all datagrid...
3
by: tirath | last post by:
hi, I have a DataView which has around 50 items(the page has scrollbars). there is a search box on top of the page. when user enters an Id, i do DataView.find("Id"). I get the row index but i...
2
by: Number 11950 - GPEMC! Replace number with 11950 | last post by:
When you highlight a selection of text represented in an object on a form, somewhere in the Framework or alternatively in the Windows API there exists an array of variables that either contain as a...
2
by: Celeste | last post by:
Hello, I'm trying to parse the referring url for google search terms so that when this page loads it will scroll to and highlight the search term(s). Should i be using document.referrer? ...
7
by: sphinney | last post by:
I have a datasheet style form with textbox (MyTextBox) that has the Text Format property set to "Rich Text". It is bound to a memo field in a table. How do I change the text or highlight color of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.