Highlighting Text In a Field

heedaf

Registered User.
Local time
Yesterday, 21:30
Joined
Jan 24, 2008
Messages
20
I'm running a search on several tables feeding into a form. When I display the search results I would like to be able to highlight the key words found in the field. For instance, if the word "purchase" was found in a description field I would like "purchase" to be highlighted and not the rest of the characters in the field. Does anyone have any idea on how to do this?
Thanks,
DeWayne
 
I assume you are seeking to highlight words in a text box on a form. You cannot highlight fields in a table. Need more information to be of real help. Maybe you could attach your tables and form.
 
Yes, only in a form. Basically, I'm just trying to highlight some of the characters in a field on a form. So if the user searches on a keyword then that keyword will be highlighted anywhere on the recordset in the form.
Thanks,
DeWayne
 
if the user searches on a keyword then that keyword will be highlighted anywhere on the recordset in the form.
I doubt that you can highlight in the datasheet view. Isn't that just like looking at a table? Ted already said that anyway.

How about using Instr() and the forecolor property here? Think there's a combination possibility there? I wonder what event you would use for that...
 
You would need an Rich Text box for doing that. A regular text box will not have multiple colors in it.
 
If you can highlight part of a field, then I hope someone puts the answer.

I would like to be able to do it with labels, just not fields.
 
Here's some code to get you started in the right direction with hiliting text in textboxes. For demo purposes:

Field being searched: DescriptionField
Search term: strSearch

In this demo, you enter a search term in strSearch then press SearchButton. If the search term is present in the the DescriptionField of the current record, it will hilite the term. For your purposes you'll need to do something like loop thru the recordset to check all records, or some such, but this should give you an idea.

Code:
Private Sub SearchButton_Click()
If InStr(Me.DescriptionField, Me.strSearch) > 0 Then
  Me.DescriptionField.SetFocus
  Me.DescriptionField.SelStart = InStr(Me.DescriptionField, strSearch) - 1
  Me.DescriptionField.SelLength = Len(strSearch)
End If
End Sub

Sorry, Mike, won't work for labels!

Linq
 
Last edited:
Here's some code to get you started in the right direction with hiliting text in textboxes. For demo purposes:

Field being searched: DescriptionField
Search term: strSearch

In this demo, you enter a search term in strSearch then press SearchButton. If the search term is present in the the DescriptionField of the current record, it will hilite the term. For your purposes you'll need to do something like loop thru the recordset to check all records, or some such, but this should give you an idea.

Code:
Private Sub SearchButton_Click()
If InStr(Me.DescriptionField, Me.strSearch) > 0 Then
  Me.DescriptionField.SetFocus
  Me.DescriptionField.SelStart = InStr(Me.DescriptionField, strSearch) - 1
  Me.DescriptionField.SelLength = Len(strSearch)
End If
End Sub

Sorry, Mike, won't work for labels!

Linq


This will work for ONE (1) highlight in ONE field for a record. You can't use it in more than one field at a time and you can't highlight every instance of the term, unless there is only ONE instance of the search term. If there are more than one, which is what it appeared that the OP was looking for, you can't do it with the code Linq wrote.
 
Here's some code to get you started in the right direction with hiliting text in textboxes... this should give you an idea.

I kind of hinted at that, didn't I Bob? I wouldn't want to take all the fun away from the OP! :D
 
I don't know that hinting in that direction is actually going to be helpful in this case. I realize that you were giving a way to highlight some text. But, it really isn't even close to where the original poster had been looking and it wouldn't have gotten them anywhere but frustrated as they tried to figure out how to highlight more than once in a field.

The only way, that I know of, is to use a Rich Text Control where you can change the colors of individual characters (or full words).
 
I am not getting anything to happen. No compile problems.....button just goes in and out:D

I made an unbound text box called strSearch. Entered a number in box strSearch, saved, clicked button

Private Sub SearchButton_Click()
If InStr(Me.FirstName, Me.strSearch) > 0 Then
Me.FirstName.SetFocus
Me.FirstName.SelStart = InStr(Me.FirstName, strSearch) - 1
Me.FirstName.SelLength = Len(strSearch)
End If

End Sub
 
Mike -

The Instr function has 3 arguments - the first is the start point, the second is the text to search and the third is the text you are trying to find in the text.
 
Got it. Just enter eni for Denis etc.

But that is not the solution. I will of course work for a filter.

I think the first poster is similar to me in that it is a true highlight as in format change that is required.
 
I think the first poster is similar to me in that it is a true highlight as in format change that is required.
He is also similiar in the fact that he probably doesn't know how to code the program properly. :D
 
Mike -

The Instr function has 3 arguments - the first is the start point, the second is the text to search and the third is the text you are trying to find in the text.
But the first argument is optional; if left blank the search starts at the first character.

Using this argument could be a way of cycling thru the entire field and hiliting each occurence.

  1. Search and hilite the first occurence.
  2. Search again, starting from the position of first occurence + length of strSearch and hiliting
  3. Repeat the above until end of field
 
I entered a number because I was thinking about Left/Right/Mid:)
Take the number out of wherever you put it. The code works as posted and described, but you really can't arbitrarily throw other stuff into it and expect it to work correctly, especially things you put in because you were "thinking about" functions that don't appear in the code!

As Bob suggested and I concurred, this example is just to give you a general idea of a way to start working on a resolution for your problem. And to be frank, the amount of work this will take will probably not be worth the end results. Exactly how big are these fields, character-wise, you want to search thru?
 

Users who are viewing this thread

Back
Top Bottom