I am working on a project which allows kids to send a message to Santa. Unfortunately, if they enter a string instead of an integer in the AGE field, the program crashes and returns Conversion from string "[exampleString]" to type 'Double' is not valid. Is there any way to check if they have entered an integer or not? This is the code.
If childAge > 0 And childAge < 150 Then
fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If
Thanks, Kai :)
You can use this.
Complementing Styxxy's response, if you dont need a result just replace it by vbNull:
In .Net you may use
GetType()
to determine the data type of a variable.Based on the above sample you can write a code snippet:
IsNumeric is built into VB, and will return a true/false
You could perform the following two tests to be reasonably certain that the input you're getting is an integer:
The InStr function returns zero if it doesn't find the string that is being looked for, and so when combining that test with IsNumeric, you also rule out the possibility that some floating point data type was entered.