?
Solved

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

Posted on 2003-03-26
5
Medium Priority
?
147 Views
Last Modified: 2010-05-01
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
Comment
Question by:sabihanna
[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
5 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 8211564
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
 
LVL 4

Expert Comment

by:trkcorp
ID: 8211567
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
 

Expert Comment

by:antidemon
ID: 8211602
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
 

Author Comment

by:sabihanna
ID: 8212216
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
 
LVL 22

Accepted Solution

by:
rspahitz earned 180 total points
ID: 8212312
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

752 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