472,143 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Count Characters on a Textbox (but while typing).

Please I would appreciate if you can help me with the correct code to countdown characters left in a TextBox.
Here is my code but doesn't work on real time, I mean, when I type in TxtBox2.

TextBox3 is where I put the counter.
TextBox2 is where it's the text.


Private Sub TextBox3_Change()
Me.TextBox3 = 250 - Len(Me.TextBox2.Text) & " Characters Left"
If Len(Me.TextBox2.Text) = 250 Then
MsgBox "No more characters allowed!"
End If
End Sub


Thanks in advance.
Nov 27 '14 #1
11 3422
Ajay Bhalala
119 64KB
I am not sure, but I think you can use timer control for count the characters while typing.
Nov 27 '14 #2
In other forum I've just read this but not sure if it will work:


Me.LblCharsLeft.Caption = 255 - Len(Trim(Me.TextBox)) & " characters left"


I need a counter on real time, so when I type in TextBox 2 need in someplace a counter that tells how many character left in TextBox2.

Thanks again.
Nov 27 '14 #3
Ajay Bhalala
119 64KB
I am sorry. You don't have to use the timer control.

You can use the following code...

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  3.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  4.         Dim a As Object
  5.         a = Len(Me.TextBox1.Text)
  6.         Label1.Text = (250 - a) & "Characters Left"
  7.     End Sub
  8. End Class
For use the above code You don't need the counter. Just simply write the above code(bloded text only) in the textchanged event of textbox in which you write the text.

I have use the one textbox and one label. I have display the characters left in the label.
Nov 27 '14 #4
Ajay Bhalala
119 64KB
I have use the one textbox in which user have to enter the characters and one label which displays the how many characters left.

Before you write this code, you must set the "maxlength" property of textbox to 250 or any value you want.

I have tried it. It works. If you write 1 character, in label text will be displayed "249 characters left"
Nov 27 '14 #5
Hi Ajay,
Thank you so much for your quick answer, but it didn't work.
I'm not very expertise with VBA and I don't understand what means all you put in the line #3 in brackets.

As I said, my text box where I have to write the text, it's called "textbox2".
If I can put the counter in the top right of the box, much nicer, otherwise let me know the best option.

When I ran your code this message appeared:
"Run time error 91
Object variable or with block variable not set"

Thanks again.
Nov 27 '14 #6
Ajay Bhalala
119 64KB
You mean to say, you are writing the text in "TextBox2" right?
Nov 27 '14 #7
Please give me the details on Label as well. Because I have added a Label1 and didn't work.

Thank you Ajay.
Nov 27 '14 #8
This is what I copied from your advice:

Private Sub TextBox2_Change()
Dim a As Object
a = Len(Me.TextBox2.Text)
Label.Text = (250 - a) & "Characters Left"
End Sub

Private Sub Label_Click()
Label.Text = (250 - a) & "Characters Left"
End Sub


and it's not working.
Nov 27 '14 #9
Ajay Bhalala
119 64KB
First you create a new project.
Then take a windows form.
Then take a one textbox in this form.
Then take a one label.

Select the textbox. The property of textbox will display.
Find the maxlength property. and write the value "250".

Double click on the textbox.

Code editor window will open. and looks like below

Public Class Form1

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

You have to write the following code after the line "Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged"

Expand|Select|Wrap|Line Numbers
  1. Dim a As Object
  2.         a = Len(Me.TextBox1.Text)
  3.         Label1.Text = (250 - a) & " Characters Left"
Do as I directed. You can understand your problem and hope can understand what I want to say.

and don't change the name of the textbox and label.
Nov 27 '14 #10
Ajay!! it worked!!! thanks a lot!!!!


Private Sub TextBox2_Change()
Me.Label1 = 250 - Len(Me.TextBox2.Text) & " Characters Left"
End Sub

Private Sub Label1_Change()

End Sub


Nov 27 '14 #11
Ajay Bhalala
119 64KB
You don't say thank you to me. I am also not expert, I am just SY B.Sc.IT student. and Glad I could be of assistance!
Nov 27 '14 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by faizankhan666 | last post: by
2 posts views Thread by si_owen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.