Always Displaying The Last Text In A Scrolling Text Box

Posted on 2009-12-29
Last Modified: 2013-11-28
Hi Experts,

I'm implimenting a simple chat application in Microsoft Access.

On a form will be the TextBox "ChatText" which will be the history of the chat session to date between two users. The ChatTextToAdd box will contain the text the user will type and then when submitted this new line will be added to the end of the ChatText text box and so on.

This works ok until the ChatText box has quite a few lines and then it disappears off the screen. Scroll bars added means the user can scroll to see the later lines but what I want is for the ChatText textbox to always display the last lines, i.e. the cursor remain at the end of the text even when the field does not have the focus.

I can use:

Forms("CHAT").Controls("ChatText").SelStart = Forms("CHAT").Controls("ChatText").SelLength

However when the focus moves away from the ChatText field and onto the ChatTextToAdd for example (when user goes to submit a new line to the chat), the ChatText tetx box reverts back to the beginning of the text and the later lines disappear again.

I have considered using a mono-spaced font and counting the length of each submitted line and the number of carriage returns etc to truncate the displayed text so that only the last few lines are display but I prefer to use a proportionately spaced font.

Any ideas anyone ?
Question by:MDIRECT
    LVL 28

    Expert Comment

    Retrieve into an integer variable the current SelLength before updating.  Then you can set the SelStart position to the integer value after updating.


    Dim intLength As Integer

    intLength = Forms("CHAT").Controls("ChatText").SelLength
    'perform you update here
    Forms("CHAT").Controls("ChatText").SelStart = intLength

    OM Gang

    Author Comment

    Hi OM Gang,

    Thanks for your comment. The problem I am having is not that setting the SelStart does not work - it does work whilst the field has the focus and the cursor happily moves to the end of the text OK.

    The problem however is keeping the same position whilst the field looses the focus. When the field has the focus the cursor can happily sit on the last character but as soon as the focus changes to another field the ChatText box displays the whole text from the very top all the time and this the bottom lines disappear off the scroll box.
    LVL 28

    Accepted Solution

    OK.  You can accomplish what you want by embedding the ChatText control in a subform and placing the subform on your main CHAT form.  When the focus moves to the ChatTextToAdd control on the main form the subform control will retain its state.  I have tested this and it works.

    I simply added an unbound text box control to an unbound form (this is the subform named Form116).  On the main form, on the AfterUpdate event of the ChatTextToAdd control run code to update the unbound text box on the subform.  Here's what I used to experiment

    Private Sub ChatTextToAdd_AfterUpdate()
    On Error GoTo Err_ChatTextToAdd_AfterUpdate

        Dim intLength As Integer
        Dim varText As Variant
        varText = "=" & Chr(34) & Me.ChatTextToAdd.Value & Chr(34)

        Me.Form116.Form.ChatText.ControlSource = varText
        intLength = Me.Form116.Form.ChatText.SelLength
        Me.Form116.Form.ChatText.SelStart = intLength

        Exit Sub

        MsgBox Err.Number & ", " & Err.Description, , "Error"
        Resume Exit_ChatTextToAdd_AfterUpdate

    End Sub

    OM Gang

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now