Solved

Prevent user enter special character in a form

Posted on 2002-05-27
3
1,703 Views
Last Modified: 2010-08-05
Dear Experts,

I have an table with two text field: [staffcode] & [staffname].
I also have a input form for user to input those data.

My question is how to prevent user entering special character " and ' on the text field.

For example, I don't want user enter [lawrence"chan"] or [law'chan'] on the text field.

Please help
0
Comment
Question by:law99
3 Comments
 
LVL 10

Expert Comment

by:RichardCorrie
ID: 7037224
use the KeyPress event to cancel the entry thus:

Private sub txtSurname_KeyPress(KeyAscii as integer)
' 39 is single quote, 34 is double quote
if KeyAscii = 39 or KeyAscii = 34 then
   KeyAscii = 0 ' null string
end if
End Sub

Beware though, this could lead to some user confusion as they keep hitting the quote key but nothing is output.

If you set the Form KeyPreview property to True you need only create a Form_KeyPress event.  This will affect all data entry on the form.

Richard
0
 
LVL 6

Accepted Solution

by:
Mach1pro earned 100 total points
ID: 7037273
Set the forms KeyPreview to true
Add the following code to the textboxes KeyUp event

If KeyCode = 222 Then
    If Shift = 1 Then
         MsgBox "Please do not use quotations"
    Else
         MsgBox "Please do not use apostrophes"
    End if
    Me.Activecontrol = Left(Me.ActiveControl.Text, Len(Me.ActiveControl.Text) - 1)
End If
0
 

Author Comment

by:law99
ID: 7037978
Althought both answers are correct, the answer of Mach1pro can give user a warning message when they input apostrophes.

This should be a better answer

Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now