By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

What event would I use to count characters WHILE typing?

P: 99
Hi,

While the user is typing into a text box, I need to display the length of the string they are typing. My code is...

displayCountLabel.caption = len(me!fieldTheyAreTypingIn)

Problem is that I can't seem to find an event that occurs while the user continuously types. I tried keydown and keyup and keypress. All of them fire on the first occurence but they do not keep firing as the user continues to type.

Does anyone know how I can do this?

Thanks
Adam
Sep 6 '08 #1
Share this Question
Share on Google+
12 Replies


missinglinq
Expert 2.5K+
P: 3,532
You have to use the Change event of your textbox. This displays the character count in a label named CharacterCountLabel.

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_Change()
  2.   CharacterCountLabel.Caption = Len(Me.YourTextBox.Text)
  3. End Sub
This method lets the user know how many characters they have left to type, then pops a warning box when they reach the limit. In this demo the limit is 255, but that number can be changes.

Place a label above or next to the textbox.
Make the label's saption "255 Characters Left"
Place this code behind the textbox:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_Change()
  2.   CharacterCountLabel.Caption = 255 - Len(Me.YourTextBox.Text) & " Characters Left"
  3.   If Len(Me.YourTextBox.Text) = 255 Then
  4.     MsgBox "No more characters allowed!"
  5.   End If
  6. End Sub
Linq ;0)>
Sep 6 '08 #2

NeoPa
Expert Mod 15k+
P: 31,347
Wow!!

Quick and impressive Linq ;)
Sep 6 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
Been asked and answered before! I keep a folder full of hacks I've written before for posts. The trick, at my advanced age, is remembering what the devil I named the file!

;0)>
Sep 6 '08 #4

NeoPa
Expert Mod 15k+
P: 31,347
For someone who should do better, I make a poor job of keeping a tally of all those too :(
Sep 6 '08 #5

P: 99
Perfect! Thanks

Interesting, when I tried referencing the text field as

me!myTextField

it didn't work. I had to use

me!myTextField.text

I always thought .text was the default.

Any way, thanks again

Adam
Sep 6 '08 #6

NeoPa
Expert Mod 15k+
P: 31,347
The default is actually Me.myTextField.Value, however, this should also return the same as .Text with text data in the control.

If you're interested I could look at the code for you. Make it clear what happens with the code you post (that doesn't work) though.
Sep 6 '08 #7

P: 99
I already typed over it and I'm not exactly sure how I had it. Besides, I don't want to put you out further. I'm up and running. Thanks!
Sep 6 '08 #8

NeoPa
Expert Mod 15k+
P: 31,347
That's fine. You're happy. That's the most important thing :)
Sep 6 '08 #9

missinglinq
Expert 2.5K+
P: 3,532
myTextField.Value is actually Null until data is saved in the field for the first time, so myTextField.Text can have data while myTextField.Value doesn't.

If you were to enter and save the word silver in myTextField, myTextField.Value would be silver. If you were to now enter the word gold in myTextField, leave the cursor in the textbox and check, myTextField.Value would still be silver even though myTextField.Text is gold and gold actually shows in the textbox.

Confusing, isn't it?

Linq ;0)>.
Sep 6 '08 #10

NeoPa
Expert Mod 15k+
P: 31,347
Of course!

This is all while still typing in the field. That makes better sense now.
Sep 6 '08 #11

missinglinq
Expert 2.5K+
P: 3,532
I think the confusion over this comes because in straight Visual Basic the Default Property of a textbox is Text, as opposed to VBA where it's Value. In retrospect, Microsoft probably would have done better to use a name like Basic For Applications, which would have yielded BFA and maybe wouldn't have been as likely to be confused with VB..

Linq ;0)>
Sep 7 '08 #12

P: 1
I am just getting to know this Access thing, and at 65 the process is equally slow.. your help with this one was great. Thank you.
Nov 7 '14 #13

Post your reply

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