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

Allow/Refuse certain values as they are entered in a form

I would like my program to be able to allow or refuse certain values as they are entered by the user.
For example, when the user has to enter an "IP Address" in the appropriate field, I would like my program to refuse it if he tries to type in anything other than numbers (i.e. letters).
This can be done either by not accepting the entry at all (as if the keyboard is jammed, with a beep sound), or by accepting initially the value and then performing a verification on the string/value entered and then displaying an error message asking the user to reenter again the value.

I would prefer the first solution, cause it forces the user to enter the right values from the begining.

Tnx
0
sabihanna
Asked:
sabihanna
1 Solution
 
rspahitzCommented:
Assuming that the input is going into a textbox:

Private Sub Text1_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
    Case vbKey0 To vbKey9
      ' allow digits
    Case vbKeyBack
      ' allow backspace
    Case Else
      KeyAscii = 0
      Beep
  End Select
End Sub

Note that this will only affect keyboard input and not mouse-pasted input, such as right-click paste.or special keyboard inputs like Shift-Insert.  To intercept those, you'll need to check every character within the Change event procedure.
0
 
trkcorpCommented:
Use the Keypress event (if, as I assume you have a text box) Example:
Private Sub txtNewC_KeyPress(KeyAscii As Integer)
'CR or Escape OK
If KeyAscii = 13 Or KeyAscii = 27 Then Exit Sub
'allow only numeric
If KeyAscii < 48 Or KeyAscii > 57 Then
     KeyAscii = 0
     BEEP
End If
End Sub
0
 
antidemonCommented:
Use a masked edit box.
set the mask to something like "999.999.999.999"

This isn't perfect.

The alternative is to place code behind the text boxes KeyPress event (here's an example of code I use to restrict to real numbers only)

If InStr(1, Text1, ".") = 0 Then
     If InStr(1, "0123456789." & Chr(8), Chr(KeyAscii)) = 0 Then KeyAscii = 0
Else
     If InStr(1, "0123456789" & Chr(8), Chr(KeyAscii)) = 0 Then KeyAscii = 0
End If


NB this first checks whether they have already typed a decimal point to decide whether to allow the entry of a decimal.

Hope this helps.
0
 
sabihannaAuthor Commented:
Thank you rspahitz for your solution. I tested it and it seems to be working fine.
Does the same solution apply for a field that is supposed to have only letters and no numbers?

Tnx

PS: I Increased the points and will accept your answer soon
0
 
rspahitzCommented:
Yes.  To allow just letters rather than numbers, change this line:

   Case vbKey0 To vbKey9

to this:

    Case vbKeyA To vbKeyZ, vbKeyLowerA To vbKeyLowerZ
and add near the top:
  Const vbKeyLowerA As Integer = 97
  Const vbKeyLowerZ As Integer = 122

You may also want to allow spaces if you're allowing letters.

Private Sub txtPath_KeyPress(KeyAscii As Integer)
  Const vbKeyLowerA As Integer = 97
  Const vbKeyLowerZ As Integer = 122
 
  Select Case KeyAscii
    'Case vbKey0 To vbKey9
      ' allow digits
    Case vbKeyA To vbKeyZ, vbKeyLowerA To vbKeyLowerZ
      ' allow upper case letters
    Case vbKeySpace
      ' allow spaces
    Case vbKeyBack
      ' allow backspace
    Case Else
      KeyAscii = 0
      Beep
  End Select
End Sub

Also note that the vbKeyX constants are actually defined for the KeyDown/KeyUp event procedures, so in some cases their values do not match those supplied by KeyASCII.  In those cases, you can do something like this:

   Case asc("$"), asc(","), asc(".")
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now