Jun 14, 2003
10,442
0
0
hey, need a little help with input boxes

my lecturer said to us that if we make a input box pop up and ask for a number, the first thing he will do is put in a letter or nothing at all and press ok to see if we have coded in error checking or whatever

what he hasnt done, despite telling us what hes gonna do many times, is told us how you do this.

this is my code

Dim intCount As Integer
Dim strCharacter As String
Dim intValue As Integer
Dim intAttempt As Integer

intCount = 0
intAttempt = InputBox("how many attemtps does one wish to have?", "input attempts")


If intAttempt < 0 Then
MsgBox ("wrong")
Exit Sub
ElseIf intAttempt = 0 Then
MsgBox ("wrong")
Exit Sub
ElseIf intAttempt = "" Then
MsgBox ("wrong")
Exit Sub
End If

now, <0 and = 0 work just fine.

but what do i put in to check for nothing at all, or to check if there is a letter in there rather than a number?
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
And input box returns a string, so I suggest you dont put the result into an integer, because if he enters a char it will throw an exception..

dim Res as string = ""
dim intRes as integer
res = InputBox("how many attemtps does one wish to have?", "input attempts")

if res <> "" and integer.tryparse(res, intRes) then
<do stuff>

tryparse is only available in 2005, othersie you'll have to ctype/parse and catch for an exception.

as for integer, i'd just to strRes.indexof(".") to see if it has a ., you might want to also check for negative numbers, but you can do that after the integer conversion.
 

KLin

Lifer
Feb 29, 2000
30,453
761
126
IsNumeric() is one way to check the input. Throw in an if/then statement to check the string that is keyed in. If it's not numeric, throw an error and exit the sub, otherwise populate the integer variable with the input value from the string.
 
Jun 14, 2003
10,442
0
0
ok thanks guys

i tried turning the intAttempt as a string, so that it would then accept the numbers or letters
then i convert that input to an ASCII code
then basically say that if its outside of 48-58 (or whatever it is that whole numbers appear) then it is to show an error box.

however, now if i actually put in a number....it goes through fine, but its in ASCII code because i have just converted it to ASCII to check to see if its in the range....however the rest of the program still seems to function correctly despite working with the ascii code for the number rather than the number itself
 
Jun 14, 2003
10,442
0
0
this is the full code


EDIT sorry fluffed it!

Private Sub cmdTutorial_3f_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim intCount As Integer
Dim strCharacter As String
Dim intValue As Integer
Dim intAttempt As String
Dim intASC As Integer

intCount = 0
intAttempt = InputBox("how many attemtps does one wish to have?", "input attempts")
intASC = Asc(intAttempt)

If intASC < 48 Or intASC > 57 Then
MsgBox ("invalid input, please use a positive integer")
Exit Sub
ElseIf intASC > 47 Or intASC < 58 Then
intAttempt = Int(intASC)
End If

If intAttempt < 0 Then
MsgBox ("enter integer greater than zero")
Exit Sub
ElseIf intAttempt = 0 Then
MsgBox ("enter integer greater than zero")
Exit Sub
End If


Do
strCharacter = InputBox("puuhhleeeesee enter a,b,c,or x to exit", "data entry box")

Select Case strCharacter

Case "a"
picTutorial_3c.Cls
picTutorial_3c.Print "You pressed a. well done"

Case "b"
picTutorial_3c.Cls

picTutorial_3c.Print "You pressed b. very well done"

Case "c"
intValue = 0
picTutorial_3c.Cls
While (intValue < 5)

picTutorial_3c.Print "you pressed c. very very well done" & intValue
intValue = intValue + 1
Wend

Case "x", "X"
Exit Do


Case Else
picTutorial_3c.Cls

picTutorial_3c.Print "you pressed a key other than a,b,c or x and are therefore a complete tool bag"
End Select

intCount = intCount + 1

Loop While (intCount < intAttempt + 1)

If Not (intCount = intAttempt + 1) Then
picTutorial_3c.Cls
picTutorial_3c.Print "you pressed x to escape this hell"

Else
picTutorial_3c.Cls
picTutorial_3c.Print "you had " & intAttempt; " attempts and failed each time."
End If

End Sub