Solved

Prevent user enter special character in a form

Posted on 2002-05-27
3
1,708 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 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

7 Experts available now in Live!

Get 1:1 Help Now