Results 1 to 11 of 11
  1. #1
    kent is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    34

    Color highlight for section of string in text box?

    I imagine this is very simple but I'm just starting with VBA and having a hard time of it. I searched but couldn't find anything helpful, which makes me think I'm searching the wrong terms:

    In a text box (set to rich text), I'd like to be able to have access highlight a portion of the text in yellow, from positions 10 to 20. This would be activated by clicking a button on the form. I've attached an image of what the text would look like.

    Any thoughts would be appreciated. Thanks!
    Attached Thumbnails Attached Thumbnails accessforum.JPG  

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Why do you want to do this with code when you can just select the text portion and format it from the ribbon?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    kent is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    34
    It's something I'm going to have to iterate in the program, but I'm just trying to figure out the basics of how to get it to highlight text.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    Well, it's HTML tags that invoke the color. You'll have to insert them into the string.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    I'd say good luck with that! Unless I've misinterpreted the goal, it's to have the code figure out which characters have been selected. So what's highlighted in the post sample gets formatted. If user selects "lazy dog" that's what gets formatted instead. I've never seen vba code that will 'capture' what screen text has been selected but I suppose there might be an API for it. Putting the tags in is not so hard. Figuring out where to put those tags based on a random selection, not so easy I'd say.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    My interpretation was that the highlight would always be from character 10 to character 20. Shouldn't be so hard to insert the start and end tags based on that.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    You probably have it right. We'll have to wait and see I guess.

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Highlighting specified text is indeed possible. I've done it many times with instances of specified words. For example:
    Click image for larger version. 

Name:	SQLSearchReport.gif 
Views:	20 
Size:	53.6 KB 
ID:	41930
    In theory such code can be modified to highlight specified characters e.g. 10-20
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  9. #9
    kent is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    34
    Quote Originally Posted by isladogs View Post
    Highlighting specified text is indeed possible. I've done it many times with instances of specified words. For example:
    Click image for larger version. 

Name:	SQLSearchReport.gif 
Views:	20 
Size:	53.6 KB 
ID:	41930
    In theory such code can be modified to highlight specified characters e.g. 10-20
    Could you point me to how you limited the highlighting to only certain words?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Have to embed HTML code tags. Can use Replace function for specific text. Such as:
    Code:
    Replace("DOB", "<font color=red>DOB</font>")
    
    Textbox must be set for Rich Text.

    Could have a table of words to highlight and VBA that loops through recordset of that table and executes Replace on a field value.




    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Basically the idea is as June said.
    The SQL for the items found in the SQL search are saved as rich text and the relevant search words are highlighted as red text with a yellow background using VBA
    This is the modified record for the first query in the report shown:

    Code:
    <div><font color="#1F497D">INSERT INTO LeaversSixthFormStudents ( PupilID, Surname, Forename, </font><font color="#800000"><font style="BACKGROUND-COLOR:#FFFF00"><strong>DOB</strong></font><font color="#1F497D">, [m/f], Yr, TGp, UPN, FormerUPN, DOA, LeavingDate, </font><font color="#800000"><font style="BACKGROUND-COLOR:#FFFF00"><strong>Active</strong></font><font color="#1F497D">, EnrolmentStatus, StudentStatus, OldPupilID, OldFullAdNo, OldImagePath ) SELECT SixthFormStudents.PupilID, SixthFormStudents.Surname, SixthFormStudents.Forename, SixthFormStudents.</font><font color="#800000"><font style="BACKGROUND-COLOR:#FFFF00"><strong>DOB</strong></font><font color="#1F497D">, SixthFormStudents.[m/f], SixthFormStudents.Yr, SixthFormStudents.TGp, SixthFormStudents.UPN, SixthFormStudents.FormerUPN, SixthFormStudents.DOA, SixthFormStudents.LeavingDate, SixthFormStudents.</font><font color="#800000"><font style="BACKGROUND-COLOR:#FFFF00"><strong>Active</strong></font><font color="#1F497D">, SixthFormStudents.EnrolmentStatus, SixthFormStudents.StudentStatus, SixthFormStudents.OldPupilID, SixthFormStudents.OldFullAdNo, SixthFormStudents.OldImagePath FROM PupilData INNER JOIN SixthFormStudents ON PupilData.PupilID = SixthFormStudents.PupilID WHERE (((PupilData.LeavingDate)<=Now()) AND ((PupilData.</font><font color="#800000"><font style="BACKGROUND-COLOR:#FFFF00"><strong>Active</strong></font><font color="#1F497D">)=No));</font></div>
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 06-26-2019, 02:27 AM
  2. Change Text color and/or background color
    By Thompyt in forum Reports
    Replies: 2
    Last Post: 02-23-2017, 07:08 PM
  3. Do not highlight text on combox rollover
    By gammaman in forum Forms
    Replies: 1
    Last Post: 12-04-2015, 08:08 AM
  4. Replies: 6
    Last Post: 05-29-2015, 10:21 AM
  5. Replies: 8
    Last Post: 05-21-2014, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums