Character Count

alforddbu

New member
Local time
Today, 15:56
Joined
Mar 22, 2012
Messages
9
I have a long, multi-line text box that has a limited number of characters. I have another text box that I'd like to display the remaining characters as the user types in the first one. I'm able to get a variable to keep the count and I'm able to get the second text box to display the character count remaining. However, it only works if I move off the record and back on to it. It is not counting dynamically as the user types (see my code below).

What am I missing?

Private Sub txtReportComment_KeyPress(KeyAscii As Integer)
On Error GoTo Err_txtReportComment_KeyPress
Dim intEntryChar, intResponseChar, intEntryLength, intResponseLength, intMaxChars As Integer

' Set initial lenght limit
intMaxChars = 255

' Count characters currently in txtReportComment text box
intEntryLength = Len(Trim(Me.txtReportComment))

' Subtract character count from initial limit and populate to the txtEntryCharacters textbox
Me.txtEntryCharacters.Value = intMaxChars - intEntryLength

Exit_txtReportComment_KeyPress:
Exit Sub

Err_txtReportComment_KeyPress:
MsgBox Err.Description
Resume Exit_txtReportComment_KeyPress

End Sub
 
Try the KeyUp Event and use Len(Trim(Me.txtReportComment.Text)).

The .Text property will get you what is in the text box at this moment.
The last key pressed will not appear in the .Text property until the KeyUp Event
 
Thanks for the info. However, the text box containing the count (Me.txtEntryCharacters.Value = intMaxChars - intEntryLength) is still not updating as I type. I need this textbox to update as the user types so they know how many characters they have left until they hit their max limit.
 
You could try something like this in the ONChange event

Private Sub YourTextBox_Change()
CharacterCountLabel.Caption = 255 - Len(Me.YourTextBox.Text) & " Characters Left"
If Len(Me.YourTextBox.Text) = 255 Then
MsgBox "No more characters allowed!"
End If
End Sub
 
In this case the OnChange event and the KeyUp event would produce practically the same result.

The only difference is that the KeyUp event would be fired for every key press (not just changes in the resulting text). With this function, though, it makes no difference.
 
This has the DoEvent you mentioned earlier.

Private Sub txtReportComment_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_txtReportComment_KeyUp
Dim intEntryChar, intResponseChar, intEntryLength, intResponseLength, intMaxChars As Integer

intMaxChars = 255
intEntryLength = Len(Trim(Me.txtReportComment.Text))
intResponseLength = Len(Trim(Me.txtResponseComment.Text))

Me.txtEntryCharacters.Value = intMaxChars - intEntryLength
Me.txtResponseCharacters.Value = intMaxChars - intResponseLength

DoEvent
Exit_txtReportComment_KeyUp:
Exit Sub
Err_txtReportComment_KeyUp:
MsgBox Err.Description
Resume Exit_txtReportComment_KeyUp

End Sub
 
I have this code in a database and it works... not sure why yours wouldn't be working

Code:
Private Sub txtReportComment_KeyUp(KeyCode As Integer, Shift As Integer)
    On Error GoTo Err_txtReportComment_KeyPress
    Dim intEntryChar, intResponseChar, intEntryLength, intResponseLength, intMaxChars As Integer
    
    ' Set initial lenght limit
    intMaxChars = 255
    
    ' Count characters currently in txtReportComment text box
    intEntryLength = Len(Trim(Me.txtReportComment.Text))
    
    ' Subtract character count from initial limit and populate to the txtEntryCharacters textbox
    Me.txtEntryCharacters.Value = intMaxChars - intEntryLength
    
    DoEvents
Exit_txtReportComment_KeyPress:
    Exit Sub
    
Err_txtReportComment_KeyPress:
    MsgBox Err.Description
    Resume Exit_txtReportComment_KeyPress
End Sub
 
Oh, you need to split up your code. You can't test both boxes out of the same KeyUp.

Looks like you added:
intResponseLength = Len(Trim(Me.txtResponseComment.Text))
Me.txtResponseCharacters.Value = intMaxChars - intResponseLength

Those need to be in the KeyUp for txtResponseComment
 
This is identical to what I have. Is there something I need to do to the counter text box that will force it to recognize the information being fed to it and react accordingly?
 
Also, you're probably not going to want the Trim function in there. If you type H-E-L-L-O-{Space} that takes up 6 characters, your expression would say it's 5.
 
This is identical to what I have. Is there something I need to do to the counter text box that will force it to recognize the information being fed to it and react accordingly?

That's what DoEvents does.
 
Here you go:

Code:
Private Sub txtReportComment_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_txtReportComment_KeyUp
Dim intEntryLength, intMaxChars As Integer

intMaxChars = 255
intEntryLength = Len(Trim(Me.txtReportComment.Text))

Me.txtEntryCharacters.Value = intMaxChars - intEntryLength

DoEvents
Exit_txtReportComment_KeyUp:
Exit Sub
Err_txtReportComment_KeyUp:
MsgBox Err.Description
Resume Exit_txtReportComment_KeyUp

End Sub

Private Sub txtResponseComment_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_txtResponseComment_KeyUp
Dim intResponseLength, intMaxChars As Integer

intMaxChars = 255
intResonselength = Len(Trim(Me.txtReportComment.Text))

Me.txtEntryCharacters.Value = intMaxChars - inresponselength

DoEvents
Exit_txtResponseComment_KeyUp:
Exit Sub
Err_txtResponseComment_KeyUp:
MsgBox Err.Description
Resume Exit_txtReportComment_KeyUp

End Sub
 
I found the problem. I was referencing the wrong text box. There's no accounting for stupidity.

Thanks, everyone, for your assistance. You gave me some great tips.
 

Users who are viewing this thread

Back
Top Bottom