Word Count Alert after reaching a number (1 Viewer)

Jen-Jen

New member
Local time
Today, 07:22
Joined
Jan 16, 2018
Messages
2
Hello

On a form I have a Word Count button for the user to click to get the number of words they are using in a memo/long text field,. We need to put a word limit because they do waffle on a bit but I understand that when using Long Text field you can't put a limit. We can't use the Text field because it has only 255 characters which is too short.

So we're instructing the users to limit their comments to 285 words.

Is there a way to have an alert pop up to warn the user that they have exceeded 285 words? That would be sooooo good!

Failing that, is there a query we can run that will help the administrator to identify those who have gone over the 285 words so that they can tell the individuals to shorten their comments?

Thank you

J
 

isladogs

MVP / VIP
Local time
Today, 07:22
Joined
Jan 14, 2017
Messages
18,218
Hello and welcome to AWF

EDIT Oops - sorry - didn't read your post properly - you wanted a word count not character count. The clue was in the thread title!!!!

The info below is for a character count.


Try the following which I use in a student report form as below:



It gives a running character count after each change & stops further entry at the set character limit (in this case = 500)

Declare the following at the top of the form module.
If you have 64-bit Access it will need modifying:

Code:
Dim ControlName As String
Const ConstSetLimitText = &HC5&
Const WM_COPY = &H301
' Modified from sources by Litwin, Getz, and Gilbert,  Stephen Lebans

'API call
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
     (ByVal hWnd As Long, ByVal wMsg As Long, _
     ByVal wParam As Long, lParam As Any) As Long
    
    Private Declare Function GetFocus Lib "user32" () As Long   'used to find window handle

You then need code for the change event of your textbox
Code:
Private Sub Report_Change()

On Error GoTo Err_Handler

    Dim SelText As String 
    SelText = SendMessage(hWnd, WM_COPY, 0, 0)
    SetReportLength
    
Exit_Handler:
    Exit Sub

Err_Handler:
    MsgBox "Error & err.Number & " in Report_Change procedure : " & Err.Description  
    PopulateErrorLog
    Resume Exit_Handler
    
End Sub

Also this procedure in the same module

Code:
Private Sub SetReportLength()

On Error GoTo Err_Handler

'called by Change event for Report
    Dim WindowHandle As Long
    Dim MaxLength As Long
    'WindowHandle = GetFocus()

'Get the window handle for this window.
    WindowHandle = GetFocus()

    MaxLength = 500 'adapt value to suit your needs

'Limit the number of characters in the Report text box.
    SendMessage WindowHandle, ConstSetLimitText, MaxLength, 0

'get text length
Const WM_GETTEXTLENGTH = &HE
'Me.Report.Properties ("hWnd")
   Me.txtCharCount = "( Characters remaining : " & MaxLength - SendMessage(WindowHandle, WM_GETTEXTLENGTH, 0, 0) & " )"
    
Exit_Handler:
    Exit Sub

Err_Handler:
    MsgBox "Error & err.Number & " in SetReportLength procedure : " & Err.Description  
    Resume Exit_Handler
    
End Sub

Hopefully I've included everything - if not let me know

P.S. If your users like to copy & paste text from Word, you will also need to prevent overlong text being imported.
In the end I add code to block users pasting into the textbox - problem solved!
 

Attachments

  • Capture.PNG
    Capture.PNG
    8.1 KB · Views: 569
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:22
Joined
May 7, 2009
Messages
19,241
hello. i also have a sample word
counting form (Form1).

initially set the limit to 10 words
but you can change it to 285.
open the Form's code in VBA and
change the value in MaxWordCount constant.

the word counter uses RegExp.
it also has a Spell checker feature.
 

Attachments

  • WordCountWithSpellCheck.zip
    32.1 KB · Views: 67

Gasman

Enthusiastic Amateur
Local time
Today, 07:22
Joined
Sep 21, 2011
Messages
14,287
Sorry arnelgp, but I have to ask

Why 285?

hello. i also have a sample word
counting form (Form1).

initially set the limit to 10 words
but you can change it to 285.
open the Form's code in VBA and
change the value in MaxWordCount constant.

the word counter uses RegExp.
it also has a Spell checker feature.
 

Minty

AWF VIP
Local time
Today, 07:22
Joined
Jul 26, 2013
Messages
10,371
@Gasman - it's the number of worms the OP wanted :)
 

missinglinq

AWF VIP
Local time
Today, 02:22
Joined
Jun 20, 2003
Messages
6,423
The OP is not really clear as to when he wants this warning to pop up...if he's happy with it occurring when the user leaves the Memo Field Control, this would do the trick:

Code:
[B]Private Sub MyMemoField_Exit(Cancel As Integer)
 
 Dim WordsEntered As Integer
 
   WordsEntered = Len(Me.MyMemoField.Text) - Len(Replace(Me.MyMemoField.Text, " ", ""))

    If WordsEntered > [COLOR="Red"]284[/COLOR] Then
      MsgBox "You've Exceeded the Allowed Number of Words!"
    End If
 
End Sub[/B]
Note that the number is the desired limit minus one.

To warn when the user actually exceeds the limit, as he/she is typing

Code:
Public Warned As Integer

Private Sub Form_Current()
  Warned = 0
End Sub

Private Sub MyMemoField_Change()

 Dim WordsEntered As Integer
 
    WordsEntered = Len(Me.MyMemoField.Text) - Len(Replace(Me.MyMemoField.Text, " ", ""))

  Me.WordCountBox = WordsEntered
  
  If Warned = 0 Then
      If WordsEntered > 285 Then
        MsgBox "You've Exceeded the Allowed Number of Words!"
        Warned = Warned + 1
    End If
  End If
  
End Sub
To warn that the limit has been exceeded, anytime the Record is accessed, the first code could be modified, dropping the .Text Property, modifying the code to account for the Memo Field being empty (Null) and placed in the Form_Current event.

Linq ;0)>
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:22
Joined
May 7, 2009
Messages
19,241
I was thinking of that but user may mustakenly or deliberately tap diubke or triple spacebar.
 

isladogs

MVP / VIP
Local time
Today, 07:22
Joined
Jan 14, 2017
Messages
18,218
@Jen-Jen
You seem to be MIA at the moment but hopefully the code provided by arnelgp & missinglinq should provide what you want.

However, I know I misread your original request, but I'd be interested to know why you want a 285 word count rather than a character count (of say 1500 characters.)

After all words do vary in length significantly .....
 

Jen-Jen

New member
Local time
Today, 07:22
Joined
Jan 16, 2018
Messages
2
Hi everyone.
Thank you so much for all you help. A lot to go over.
Sorry about the 285 Words. It was just that I put in a Word count control from another database and 285 words fitted in nicely ! I also thought it gives the user more of an idea.

I'm always so chuffed with myself when I design stuff on Access but then I read all the stuff that you guys do and realise I know nuffink!!! :confused: I'm in awe of you all.

I shall now go and try out everything you suggested. I'm sure to be back with questions.


Jen
 

jdraw

Super Moderator
Staff member
Local time
Today, 02:22
Joined
Jan 23, 2006
Messages
15,379
Jen,

I have seen systems where people/developers have created free text areas(similar to your comments) for users to enter information. Many times it is appropriate and is some anecdotal text for clarity. Other times, the "free text" is really used to identify some attribute or add information which would be better served by means of selecting options from a list. For example -- options for color; or some quantifiable topic (poor, acceptable, satisfactory, excellent....).

The 285 word count seems very arbitrary (as you have mentioned). Often, as ridders said, there is a max character count on such text areas.

Any way, glad you have it resolved.
 
Last edited:

Users who are viewing this thread

Top Bottom