sabihanna
asked on
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
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
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
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
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.
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.