×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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?

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

Use the IsNumeric function to check the input before using it.
And put some error handling in to prevent the crashes.

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

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: www.essexsteam.co.uk

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

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources