Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsNumeric String Validation

Status
Not open for further replies.

Developerman2000

Programmer
Dec 9, 2002
10
GB
I am trying to validate textbox entry into a basebase program so the user can only enter Numeric Values. I has the IsNumeric validation is work ok as below.

Dim checkKeyPR As String
Dim checkEntry As String

checkKeyPR = txtPrice.Text
If txtPrice.Text = "" Then Exit Sub
If (Not IsNumeric(checkKeyPR)) Then
checkEntry = Left(checkKeyPR, Len(checkKeyPR) - 1)
MsgBox "You can only enter a Numeric Value!"
txtPrice.Text = checkEntry

End If

SendKeys "{end}"

But When you enter a NonNumeric value into the middle of a Entry it Deletes the incorrect entry but also deletes all numeric values to the RIGHT of the TextBox!

Any Ideas?
 
Hi,
Try using the KeyPress event to trap the key pressed. If a non-numeric value is entered, set keyascii=0. Additionally you can display an error message also to the user. Hope it helps. Let me know what happens.
With regards,
PGK
 
Building on what pgk posted you can have something similar to this.

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 And KeyAscii > 57 Then
KeyAscii = 0
MsgBox &quot;Enter numbers only.&quot;
End If
End Sub
Thanks and Good Luck!

zemp
 
Building on what has already been said, in place of the message box, I recommend simply using the Beep command to tell the user that the keystroke is unacceptable. We have used this in several applications and the users prefer it to having to respond to a messagebox. If the prompt for the textbox clearly indicates that it should be a number, it isn't a big deal to avoid displaying the message. BlackburnKL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top