Question : 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 ?
 

Answer : Always Displaying The Last Text In A Scrolling Text 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
   
    Me.Form116.Form.ChatText.SetFocus
   
    intLength = Me.Form116.Form.ChatText.SelLength
    Me.Form116.Form.ChatText.SelStart = intLength

Exit_ChatTextToAdd_AfterUpdate:
    Exit Sub

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

End Sub


OM Gang
Random Solutions  
 
programming4us programming4us