• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Input control of entry

I have declared a text for lastname, in my entry I don't like a numbers and special characters to appear as I type the name. How can I control this?
0
KENTROSE
Asked:
KENTROSE
  • 4
  • 3
  • 2
  • +2
1 Solution
 
adityauCommented:
In the KeyPress event of the text box, enter the following code.

If Not (KeyAscii >= vbKeyA And KeyAscii <= vbKeyZ) And Not (KeyAscii >= 97 And KeyAscii <= 122) And KeyAscii <> vbKeyBack Then  
    KeyAscii = 0
End If
0
 
caraf_gCommented:
<and a user comes along who copies rubbish in and pastes it into the text box with the mouse>
0
 
adityauCommented:
caraf_g, he is just asking for validation when he types. If he wants to validate when copy, paste etc., he has to handle the text box change event or else disable the option by subclassing.

Kentrose, did my answer solve my problem?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
caraf_gCommented:
No worries: I suppose it depends on how foolproof you want this to be. It's up to KENTROSE to let us know more. (And increase the number of points to something more reasonable <g>)
0
 
SiM99Commented:
put the code into the _change event of the textbox, rather than the keypress event....

either check the ascii value of all of the characters in the string (textbox.text) or just check the ascii value of the last character typed (right$(textbox.text)) to see if it's different to those ascii values of A-Z and a-z, if so, then either remove that character or change it.
0
 
BMMMAILCommented:
Kentrose might increase the points for this question.
0
 
KENTROSEAuthor Commented:
Thanks for the answer, it help me.
0
 
BMMMAILCommented:
adityau,
 I have also tried your answer in my owmn application, I add how about for the comma (,), the employee name is not separated as lastname, firstname and mi, it is just one text box. I want lastname separated by comma. There was no vbkeycomma , what is the ascii number value for this?
0
 
SiM99Commented:
to find the ascii value of any character, use the asc() function....

e.g. msgbox asc(",")
0
 
adityauCommented:
BMMMAIL

Answer is 44
0
 
caraf_gCommented:
This is a question that, in many variations, comes up regularly.

I wrote a Usercontrol that will allow you to do this sort of stuff, plus it handles all possible mouse and keyboard actions a user could try.

Interested, anyone?
0
 
KENTROSEAuthor Commented:
May you send me that code, it will help in my application.
0
 
caraf_gCommented:
Try this:
http://homepage.eircom.net/~carafa/VBSamples/VB6/SmartBox.zip

Create a new folder and extract the contents of the zip file into it.

Then open Group1.vbp

This project group contains two projects, one (PCSmartBox) will compile as an OCX, the other (Project1) is just a test project to illustrate how it works.

SmartBox takes care of all the nasty stuff with regards to handling mouse and keyboard action and any way the user may try to cut, copy or paste.

The only thing the text project needs to do is to code the CheckValue procedure.

In the example I've coded the CheckValue in such a way that the box will under no circumstances allow the letter "a" to appear in the box. Just a daft example ;-)

To allow proper numeric value, you just need to code "IsNumeric" in the box.

PS - the labels on the form in Project1 are a bit misleading. Nothing to do with numeric... But... try typing in an A in the smartbox. Then type an a in the text box, copy it and try to paste it. Shift+Ins, Ctrl+V, right-click with the mouse, it won't let you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now