Solved

Prevent user enter special character in a form

Posted on 2002-05-27
3
1,713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

631 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