Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   Inputbox (http://www.go4expert.com/forums/inputbox-t17136/)

stuka 26Apr2009 06:50

Inputbox
 
ok im having trouble with my imput box it crashes when i enter text, nothin and when i press the cancel button. i do realise when i click the cancel buttin it returns a null value and vb doesnt like it but why does it crash with letters or typing nothin because i have put if it = "" then.... shouldnt that mean it will accept nothin entered?

heres one of the input boxes maybe u can tell what im doin wrong and how to fix it.

Code:

If question1 = 1 And level = 0 And x >= 134 And x < 275 Then
            Form1.txtlevel.Text = 1
            x = 134
            aboard.Draw(g)
            Me.Draw(g)

            Dim Q As String
            Dim number1 As Short
            Dim number2 As Short
            Dim result As Short

            number1 = Int((100 * Rnd()) + 1)
            number2 = Int((100 * Rnd()) + 1)
            Q = InputBox("what is " & number1 & " + " & number2 & " ?")
            Randomize()

            result = number1 + number2
            If Q = result Then
                MsgBox("You got it right!")
                question1 = 2
                Form1.Txtquestion1.Text = 2

            ElseIf Q = "" Then
                Form1.Txtlives.Text = Form1.Txtlives.Text - 1
                MsgBox("Correct answer is: " & result & ", Your going back!")
                MsgBox("you Lost of life you have " & Form1.Txtlives.Text & " left")
                x = 134 - 120
                aboard.Draw(g)
                Me.Draw(g)

            Else
                Form1.Txtlives.Text = Form1.Txtlives.Text - 1
                MsgBox("Correct answer is: " & result & ", Your going back!")
                MsgBox("you Lost of life you have " & Form1.Txtlives.Text & " left")
                x = 134 - 120
                aboard.Draw(g)
                Me.Draw(g)
          End if


nimesh 26Apr2009 13:39

Re: Inputbox
 
The code looks clean to me and according to me it works if you write
If Q = ""
Did you try debugging?
Does it crash at this statement?

You can try using vbNull or just NULL, whichever works correctly.
ElseIf Q = vbNull Then
or
ElseIf Q = Null Then

Let me know the outcome.

- Nimesh

stuka 26Apr2009 23:24

Re: Inputbox
 
i got a reply on another forum and that way seems to have worked perfect plus made the code alot shorter but thanks for your reply :)


All times are GMT +5.5. The time now is 07:52.