Character Length (1 Viewer)

kitty77

Registered User.
Local time
Today, 11:56
Joined
May 27, 2019
Messages
715
I'm using the following code to notify the user that they can only use a certain amount of characters.
How can I prevent them from continuing instead of just a text box?

If (Len(Msite) > 29) Then MsgBox "Text too long - Max 29 Characters", vbOKOnly
 
Which event are you using for this?
 
You can lock every other input until they do.

You can delete their input and make it a required field so nothing gets saved until they do it right.

You can pop up another form with just one input that feeds back into that one which won't go away until they input correctly.

You could truncate their input to the first 29 characters

You could have it send an email to you and you show up with a crowbar.
 
I just want a simple msgbox alerting them but not let them continue. Nothing super secure.
 
Investigate using the key down event

On my phone so air code

if len(mycontrol.text)>29 then char=asc(0)

Or set the field length to 29

you may also need code to allow for copy/paste
 
Cannot use after update because that happens after leaving the textbox not after entering data. Use on Change (happens after each keystroke) or as mentioned maybe the keydown.
The AfterUpdate event is triggered when a control or record is updated. Within a record, changed data in each control is updated when the control loses the focus or when the user presses Enter or Tab.
 
in the field's before update event, test the length entered, and cancel the event until they fix it.
Code:
if len(fieldname)>maxpermitted then
   msgbox "Sorry. Too long. The maximum length is " & maxpermitted & " characters. "
  cancel = true
  exit sub
end if
 
The change event will work but I would still use the control's BeforeUpdate event. Only when he tabs out of a control do we know when he is done with the typing. Cancelling the BeforeUpdate event will prevent the user from leaving the control if it contains an invalid value.

The difference is that the code in the Change event runs for every keystroke but the code for the BeforeUpdate event runs only once. Also, with the code in the Change event, you can prevent typing any character after 28 have been entered rather than allowing the extra keystrokes and not complaining until the user says he is finished with data entry in the control.

Technically @gemma-the-husky 's suggested code could exist in either event and both would achieve the desired result. But due to my former life in the mainframe world, I abhor the idea of running any code 29 times when running it once will suffice.

For purposes of validation, I would NEVER, EVER use an event that does not provide the Cancel option. You can display as many messages as you want but using an event without the Cancel option never actually stops the data from being saved.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom