Solved

Prevent user enter special character in a form

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

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…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 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