Consulting

Results 1 to 6 of 6

Thread: VBA to highlight textbox

  1. #1

    VBA to highlight textbox

    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:



    to:


    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

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,288
    Location
    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

  3. #3
    Quote Originally Posted by OBP View Post
    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?

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,288
    Location
    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.

  5. #5
    Quote Originally Posted by OBP View Post
    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)

  6. #6
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,288
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •