Available Characters

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Available Characters

Post by adam »

Hi anyone,

The following code restricts the user from writing more than 10 characters in the textbox.

Code: Select all

Private Sub txtNotes_Change()
    If txtNotes.TextLength > 10 Then
        MsgBox "Too long"
        txtNotes.Text = Left(txtNotes.Text, txtNotes.TextLength - 1)
    End If
End Sub
When the User Form gets loaded, the textbox txtMessageLimit gets loaded with the number 10 using the following line

Code: Select all

Private Sub UserForm_Initialize()
    Me.txtMessageLimit.Value = 10
End Sub
As the user type’s characters in textbox textNotes, the used characters should get displayed in the txtMessageCharacters.

And the textbox txtAvailableCharacters should be textbox txtMessageLimit – textbox txtMessageCharacters

Code: Select all

Me.txtAvailableCharacters  = Me.txtMessageLimit - Me.TtxtMessageCharacters
How could I put this line to the code at the begging of this post to achieve what is mentioned.

Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78410
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Available Characters

Post by HansV »

Like this:

Code: Select all

Private Sub txtNotes_Change()
    If txtNotes.TextLength > 10 Then
        MsgBox "Too long"
        txtNotes.Text = Left(txtNotes.Text, 10)
    End If
    Me.txtMessageCharacters.Value = Me.txtNotes.TextLength
    Me.txtAvailableCharacters = Me.txtMessageLimit - Me.txtMessageCharacters
End Sub
(I would probably use labels instead of text boxes for the limit, number of used characters and number of available characters)
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Available Characters

Post by adam »

Thankyou very much Hans. It worked as needed. I'll change the textboxes into labels.
Best Regards,
Adam