• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Always Displaying The Last Text In A Scrolling Text Box

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 ?
  • 2
1 Solution
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
MDIRECTAuthor Commented:
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.
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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now