Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

VB: how do I prevent crashing if a numeric input is a letter 1

Status
Not open for further replies.

MrMiller

Aerospace
Jan 30, 2004
54
I am writing a VB program and am in the process of idiot proofing it. I have a section that requests a numeric input then does some calculations later with that number. I would like to prevent the calculation from taking place if a non-numeric character has been entered.

How do I do this?
 
Replies continue below

Recommended for you

Use the IsNumeric function to check the input before using it.
And put some error handling in to prevent the crashes.
 
I process individual keystrokes in the KeyPress event of the textbox receiving the input:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8, 46, 48 To 57 'Backspace, decimal point & 0-9
Exit Sub
Case Else
    KeyAscii = 0
    Beep
End Select
End Sub

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

UK steam enthusiasts:
 
Thanks guys. I was able to do it with your help. I'm fairly new to the VB world as you might of guessed.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor