PDA

View Full Version : VBA to highlight textbox



vinzanitee
02-24-2021, 05:45 AM
Hi Team, desperately need your expertise. I have this code that will highlight the keyword result query. This time I want to highlight the textbox of the results instead of the text itself. Thank you in advance!

From:
https://www.access-programmers.co.uk/forums/attachments/capture-jpg.89483/


to:
https://www.access-programmers.co.uk/forums/attachments/4-jpg.89484/


Public gPhoneFltr As String

Public Const FONT_COLOR = "#FFFFFF"
Public Const FONT_BGCOLOR = "#FF6600"




Public Const gBasicHTMLTemplate = "<div>{3}<font color={1} style=""BACKGROUND-COLOR:{2}"">{4}</font>{5}</div>"


Public Function getPhoneFltr() As String
getPhoneFltr = gPhoneFltr
End Function


Public Function getBasicHTMLTemplate() As String
getBasicHTMLTemplate = gBasicHTMLTemplate
End Function




Public Function formatMatch(ByRef oValue As Variant, ByRef oMatch As Variant, ByRef oTemplate As Variant) As String


Dim sValue As String
Dim sMatch As String
Dim sTemplate As String
Dim iPos As String

Dim beforeMatch As String
Dim onMatch As String
Dim afterMatch As String

sValue = Nz(oValue, "")
sMatch = Nz(oMatch, "")
sTemplate = Nz(oTemplate, "")
formatMatch = sValue


If Len(sValue) > 0 And Len(sMatch) > 0 And Len(sTemplate) > 0 Then
iPos = InStr(1, sValue, sMatch, vbDatabaseCompare)


If iPos > 0 Then
iPos = iPos - 1
formatMatch = sTemplate

' Color
formatMatch = Replace(formatMatch, "{1}", FONT_COLOR)
formatMatch = Replace(formatMatch, "{2}", FONT_BGCOLOR)



' If sValue = sMatch Then
' formatMatch = Replace(formatMatch, "{3}", "")
' onMatch = Mid(sValue, 0, Len(sMatch))
' formatMatch = Replace(formatMatch, "{4}", onMatch)
' formatMatch = Replace(formatMatch, "{5}", "")
' Else

' Before Match
beforeMatch = Mid(sValue, 1, iPos)
If iPos - 1 > 0 Then
formatMatch = Replace(formatMatch, "{3}", beforeMatch) 'Left(sValue, iPos))
Else
formatMatch = Replace(formatMatch, "{3}", "")
End If

' Match
onMatch = Mid(sValue, iPos + 1, Len(sMatch))
formatMatch = Replace(formatMatch, "{4}", onMatch)

' After Match
afterMatch = Mid(sValue, iPos + 1 + Len(sMatch))
If (iPos - 1) + Len(sMatch) < Len(sValue) Then
formatMatch = Replace(formatMatch, "{5}", afterMatch)
Else
formatMatch = Replace(formatMatch, "{5}", "")
End If
End If
' End If
End If

End Function

OBP
02-24-2021, 07:28 AM
That looks like a lot of code to change the background colour.
You can do it with
Me.fieldname.BackColor = 4033390 'where fieldname is the name of your field
Or you can use
very light green = 7405514
Light green = 6876860
slightly darker = 5950882
Very dark green = 3107669

vinzanitee
02-24-2021, 08:23 AM
That looks like a lot of code to change the background colour.
You can do it with
Me.fieldname.BackColor = 4033390 'where fieldname is the name of your field
Or you can use
very light green = 7405514
Light green = 6876860
slightly darker = 5950882
Very dark green = 3107669

thank you for your reply. which part of the code i will replace?

OBP
02-24-2021, 08:41 AM
That would depend on where and how the code is being used, is it on one form or multiple forms, I should have read your question more closely, as you appear to be using it in a query.

vinzanitee
02-24-2021, 08:52 AM
That would depend on where and how the code is being used, is it on one form or multiple forms, I should have read your question more closely, as you appear to be using it in a query.


i guess this is the part where the highlight of text happens:


' Color formatMatch = Replace(formatMatch, "{1}", FONT_COLOR) formatMatch = Replace(formatMatch, "{2}", FONT_BGCOLOR)

OBP
02-24-2021, 09:24 AM
I meant where abouts in the database it was being used.
Also the colour appears to be dependent upon the value oValue passed to the code, I have to admit I have never seen the query font manipulated before.
So I assume that the code is changing the font in a query itself, which is unusual, as I would display the output in a form or Report.
You may not be able to set the back clolour in a query, wheres you can in forms and reports.