Best way to disallow special characters in a field (1 Viewer)

PaulD2019

Registered User.
Local time
Today, 02:02
Joined
Nov 23, 2019
Messages
75
Hi all, I have done a bit of searching online, didn't really see a what would be the best way to do what I need

One of the fields on one of the forms on one of the databases I have created I want to use its contents as part of the file name when a pdf is created, with it being used as part of the file name I need to disallow the special characters \ / : * ? " < > |

I was thinking having a box popping up either when it is typed if that is possible or before the form is closed

What would be the best way to achieve this?

Thanks in advance
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:02
Joined
Oct 29, 2018
Messages
21,453
Hi. Not sure if it's the "best" approach, but one way is to use the Change event of the Textbox.
 

Micron

AWF VIP
Local time
Yesterday, 21:02
Joined
Oct 20, 2018
Messages
3,478
You have 3 options that I know of:
- Change event: validate every keystroke against your list of allowed or disallowed characters. Validation is immediate.
- control BeforeUpdate event: validate entire contents of textbox before user action completes its update; cancel update if pass fails
- form BeforeUpdate event: validate the entire form, especially this field, cancel as before

Another way (which is just a guess, thus I didn't include it above) is to let Windows handle the errors. However, your list of prohibited characters might be more inclusive, plus I don't know if you can trap the error in Access when it occurs.
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:02
Joined
Mar 14, 2017
Messages
8,774
If you use the Change event of the Textbox (which is what I would probably do as has been suggested), you should refer to it's .Text property
 

PaulD2019

Registered User.
Local time
Today, 02:02
Joined
Nov 23, 2019
Messages
75
Thanks all, I'll look into adding some VBA code to the fields OnChange event
 
Last edited:

Dreamweaver

Well-known member
Local time
Today, 02:02
Joined
Nov 28, 2005
Messages
2,466
In my tab but You could use the key press event if one of the characters is used set ascii=0 I'm not sure what the codes are for each key but you could use a debug.print ascii to find out
 

Micron

AWF VIP
Local time
Yesterday, 21:02
Joined
Oct 20, 2018
Messages
3,478
The code at that link will not allow characters that have not been defined as 'not allowed' by OP. Just sayin so that it's not used verbatim.
 

Users who are viewing this thread

Top Bottom