1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Inputbox

Discussion in 'Visual Basic ( VB )' started by stuka, Apr 26, 2009.

  1. stuka

    stuka New Member

    Joined:
    Apr 19, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    770
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    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
     
    shabbir likes this.
  3. stuka

    stuka New Member

    Joined:
    Apr 19, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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 :)
     

Share This Page