Character Length (1 Viewer)

kitty77

Registered User.
Local time
Today, 15:17
Joined
May 27, 2019
Messages
712
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:17
Joined
Oct 29, 2018
Messages
21,474
Which event are you using for this?
 

plog

Banishment Pending
Local time
Today, 14:17
Joined
May 11, 2011
Messages
11,646
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.
 

kitty77

Registered User.
Local time
Today, 15:17
Joined
May 27, 2019
Messages
712
I just want a simple msgbox alerting them but not let them continue. Nothing super secure.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:17
Joined
Feb 19, 2013
Messages
16,616
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:17
Joined
May 21, 2018
Messages
8,529
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.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:17
Joined
Sep 12, 2006
Messages
15,657
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
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:17
Joined
Feb 19, 2002
Messages
43,280
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

Top Bottom