Charater Counter

smbarney

Registered User.
Local time
Today, 03:53
Joined
Jun 7, 2006
Messages
60
All...

Looking for some suggestions. I use the code (below) to give users a running count of the text they have typed in a field. It writes the results to an unbound text box called CharCounter.

It works just fine. However, I've got dozens of text fields (60 altogether, actually) that I need to use this in and a dozen or so memo fields.

What I want is a way to call on a global function in the On Key up event (or where ever it would work best) that does the following:

  • Totals the number of the characters typed into a field
  • Gives the number of remaining characters based on the length indicated in the table
  • And makes character count always visible--with the current way I do this, the count goes way if you move to a different record and come back.
I have tried to figure this out and having managed to yet. I would very much appreciate some help on this one.
BTW: I am using Access 2007
Thanks


Code:
Private Sub Summary_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.Summary.Text))
    
    ' Subtract character count from initial limit and populate to the txtEntryCharacters textbox
    Me.CharCounter.Value = intMaxChars - intEntryLength
    
    DoEvents
Exit_txtReportComment_KeyPress:
    Exit Sub
    
Err_txtReportComment_KeyPress:
    MsgBox Err.Description
    Resume Exit_txtReportComment_KeyPress
 
Hi

you can pass the control name to the function and call the function on the key up event

Cheers


Nigel
 
hello,
I use a function to create self-filtering combos using the "on key up" event. It relies on the "screen" object. This object allows you to reference the active textbox that you are typing in. I modified your code into a function, and you cn reference this function by placing the expression "=CalculateTextLength()" for each of the 60 of your textboxes' onkeyup property. Let me know if the code doesn't work, I haven't tried it in VBE.

Code:
Private function CalculateTextLength()
On Error GoTo Err_txtReportComment_KeyPress
Dim intEntryChar, intResponseChar, intEntryLength, intResponseLength, intMaxChars As Integer
dim txtCurrentBox as textbox
' Set initial lenght limit     intMaxChars = 255     
'get a reference to the active textbox
set txtCurrentBox = screen.activecontrol
 ' Count characters currently in txtCurrentBox      intEntryLength = Len(Trim(txtCurrentBox))          ' Subtract character count from initial limit and populate to the txtEntryCharacters textbox     Me.CharCounter.Value = intMaxChars - intEntryLength          DoEvents Exit_txtReportComment_KeyPress:     Exit Sub      Err_txtReportComment_KeyPress:     MsgBox Err.Description     Resume Exit_txtReportComment_KeyPress
btw, you are aware that among all those variables you declare above that only intMaxChars is declared as an integer, and the rest are variants, right? I'm pretty sure you intended to declare them as integers but in vba you have to include "as integer" after each variable.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom