Results 1 to 13 of 13

Thread: how to detect: input nothing

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27

    how to detect: input nothing

    I want to use IF-THEN rules to detect users entering nothing in these forms text box by showing a message box.

    but how?... I know how to do a message box, but in oder to run the message box, my program has to detect that the user hasn't entered anything?

    please help........... thank you

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If (Answer.Text = "water") Then 'If correct answer then'
    6.             Users_score = Users_score + 1 'add one to the score'
    7.         End If
    8.         Unload Q1 'closes Question 1 form'
    9.         Ans_1.Show 'loads Answer and comment form for question 1'
    10.     End If
    11. End Sub
    12. 'This code runs when the user has entered their answer in the box provided in the form and clicked OK, it checks if the answer is numbers or not, if so then it will show a message box to the user and if not, then continue the program,, if the answer is correct then add one to the score then closes the question page and load the answer and comment page.'

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If Not (LCase(Answer.Text) = LCase("East Germany") Or LCase(Answer.Text) = LCase("West Germany") Or LCase(Answer.Text) = LCase("no man's land")) Then 'if the answer is not East Germany, West Germany or no man's land, then add one to the score.'
    6.             Users_score = Users_score + 1
    7.         End If
    8.         Unload Q4 'closes Question 4 form'
    9.         Ans_4.Show 'loads Answer and comment form for question 4'
    10.     End If
    11. End Sub
    12. 'This code runs when the user has entered their answer in the box provided in the form and clicked OK, it checks if the answer is numbers or not, if so then it will show a message box to the user and if not, then continue the program, close the question page and load the answer and comment page, also if the answer is not East Germany, West Germany or no man's land, then it will add one to the score.'

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         Users_score = Users_score + 1 'add one to the score'
    6.         Unload Q6 'closes Question 1 form'
    7.         Finish_page.Show 'loads Answer and comment form for question 1'
    8.     End If
    9. End Sub
    10. 'This code runs when the user has entered their answer in the box provided in the form and clicked OK, it checks if the answer is numbers or not, if so then it will show a message box to the user and if not, then continue the program, and add one to the score, closes the question page and load the answer and comment page.'

  2. #2
    Addicted Member jewel's Avatar
    Join Date
    Jul 2003
    Location
    truly asia
    Posts
    153
    hi

    try:

    Private Sub Continue_Click()
    if Answer.Text = "" then
    msgbox "Please type your answer here"
    Answer.Setfocus
    exit sub
    end if
    end sub
    xoxo

  3. #3
    Addicted Member
    Join Date
    Aug 2002
    Posts
    187
    If you have to test for an empty string in many text boxes, you might consider using a function. A nice one I picked up from this forum (cannot remember who wrote it) is:

    VB Code:
    1. copy into a bas module:
    2.  
    3. Public Function IsTextEmpty(ByVal strText As String) As Boolean
    4. IsTextEmpty = CBool(Len(Trim$(strText)) = 0)
    5. End Function
    6.  
    7. To use in a form:
    8.  
    9. If IsTextEmpty(Textbox1.Text) then
    10. Msgbox "No data entered"
    11. End if


    Cheers

    Jack

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27
    could there be a more simple way?

    btw, thx jewel but it didn't work...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27
    Okay I did what Jewel said but it didin't work...

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If Answer.Text = "" Then
    6.             MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
    7.         Else
    8.             If (Answer.Text = "bread") Then 'If correct answer then'
    9.                 Users_score = Users_score + 1 'add one to the score'
    10.             End If
    11.         End If
    12.     End If
    13.         Unload Q2 'closes Question 1 form'
    14.         Ans_2.Show 'loads Answer and comment form for question 1'
    15. End Sub

  6. #6
    Addicted Member jewel's Avatar
    Join Date
    Jul 2003
    Location
    truly asia
    Posts
    153

    Okay I did what Jewel said but it didin't work...


    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If Answer.Text = "" Then
    6.             MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
    7.         Else
    8.             If (Answer.Text = "bread") Then 'If correct answer then'
    9.                 Users_score = Users_score + 1 'add one to the score'
    10.             End If
    11.         End If
    12.     End If
    13.         Unload Q2 'closes Question 1 form'
    14.         Ans_2.Show 'loads Answer and comment form for question 1'
    15. End Sub



    The first code I sent you works fine for me maybe we can revised your code to this:

    VB Code:
    1. Private Sub Continue_Click()
    2.    
    3. '------------------------------
    4. '    validate first input
    5.     If Answer.Text = "" Then
    6.         MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
    7.         Answer.SetFocus
    8.         Exit Sub
    9.     End If
    10.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    11.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    12.         Answer.SetFocus
    13.         Exit Sub
    14.     End If
    15.     '---------------------------------------------
    16.     If (Answer.Text = "bread") Then 'If correct answer then'
    17.         Users_score = Users_score + 1 'add one to the score'
    18.     End If
    19.    
    20.     Unload Q2 'closes Question 1 form'
    21.     Ans_2.Show 'loads Answer and comment form for question 1'
    22. End Sub

    or you can make a function returning true if the input is valid like Jackalx25 did

    VB Code:
    1. Private Sub Continue_Click()
    2.    If IsValid(Answer.Text) = True Then
    3.         If (Answer.Text = "bread") Then 'If correct answer then'
    4.             Users_score = Users_score + 1 'add one to the score'
    5.         End If
    6.     Else
    7.         MsgBox "Invalid Answer!!"
    8.         Answer.SetFocus
    9.     End If
    10.     Unload Q2 'closes Question 1 form'
    11.     Ans_2.Show 'loads Answer and comment form for question 1'
    12. End Sub
    13.  
    14. Private Function IsValid(str As String) As Boolean
    15.     IsValid = True
    16.     If Answer.Text = "" Or IsNumeric(Answer.Text) = True Then
    17.         IsValid = False
    18.     End If
    19. End Function
    xoxo

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27
    wait wait wait..... my own little mistakes... it actually works... but after the message box, it skips to the other form... how can i make it not do that?... and let the user answer the question? after they have entered nothing and the message box poped up?

  8. #8
    Addicted Member jewel's Avatar
    Join Date
    Jul 2003
    Location
    truly asia
    Posts
    153
    Originally posted by Blitzen
    wait wait wait..... my own little mistakes... it actually works... but after the message box, it skips to the other form... how can i make it not do that?... and let the user answer the question? after they have entered nothing and the message box poped up?
    hi
    please make sure you put exit sub after you show your msgbox

    VB Code:
    1. If Answer.Text = "" then
    2.       msgbox "Please Enter something"
    3.       Answer.Setfocus
    4.      [B]Exit Sub[/B] 'it will ignore all the code after this
    5. end if
    hope it wors
    xoxo

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27
    well other questions forms worked exept this one and the coding is

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If Not (LCase(Answer.Text) = LCase("East Germany") Or LCase(Answer.Text) = LCase("West Germany") Or LCase(Answer.Text) = LCase("no man's land")) Then 'if the answer is not East Germany, West Germany or no man's land, then add one to the score.'
    6.             Users_score = Users_score + 1
    7.         Else
    8.             If (Answer.Text = "") Then
    9.                 MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
    10.                 Answer.SetFocus
    11.                 Exit Sub 'it will ignore all the code after this'
    12.                 End If
    13.         Unload Q4 'closes Question form'
    14.         Ans_4.Show 'loads Answer and comment form'
    15.         End If
    16.     End If
    17. End Sub

    I enter nothing and the OK command button did not work, like it showed it had been pressed. it should pop up a msgBox but nothing happened... why?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27
    sorry... my little mistakes again........

    placed the new coding in the wrong places.....

    well, it worked and my program has finally completed !!!

    thank-you VB Forum members for all your help

  11. #11
    Addicted Member jewel's Avatar
    Join Date
    Jul 2003
    Location
    truly asia
    Posts
    153
    well, i run your program and this is what i found out
    your condition

    VB Code:
    1. If Not (LCase(Answer.Text) = LCase("East Germany") Or LCase(Answer.Text) = LCase("West Germany") Or LCase(Answer.Text) = LCase("no man's land")) Then


    is true so its proceeds to
    VB Code:
    1. Users_score = Users_score + 1

    and ignores your validation... to prove this try tracing you program by pressing F9 on you if condition (you make a breakpoint] and then run your program, press F8 to step into the next line




    Originally posted by Blitzen
    well other questions forms worked exept this one and the coding is

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If Not (LCase(Answer.Text) = LCase("East Germany") Or LCase(Answer.Text) = LCase("West Germany") Or LCase(Answer.Text) = LCase("no man's land")) Then 'if the answer is not East Germany, West Germany or no man's land, then add one to the score.'
    6.             Users_score = Users_score + 1
    7.         Else
    8.             If (Answer.Text = "") Then
    9.                 MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
    10.                 Answer.SetFocus
    11.                 Exit Sub 'it will ignore all the code after this'
    12.                 End If
    13.         Unload Q4 'closes Question form'
    14.         Ans_4.Show 'loads Answer and comment form'
    15.         End If
    16.     End If
    17. End Sub

    I enter nothing and the OK command button did not work, like it showed it had been pressed. it should pop up a msgBox but nothing happened... why?
    xoxo

  12. #12
    Addicted Member jewel's Avatar
    Join Date
    Jul 2003
    Location
    truly asia
    Posts
    153
    Originally posted by Blitzen
    sorry... my little mistakes again........

    placed the new coding in the wrong places.....

    well, it worked and my program has finally completed !!!

    thank-you VB Forum members for all your help
    Gud!!!
    xoxo

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    27
    okay I need some explaination...

    VB Code:
    1. Private Sub Continue_Click()
    2.     If IsNumeric(Answer.Text) = True Then 'check for correct data type'
    3.         MsgBox "You've answered in the wrong data type, please enter in alphabet letters", 1 + 48, "Error" 'message box shown if the user enters their answer in numbers'
    4.     Else
    5.         If (Answer.Text = "water") Then 'If correct answer then'
    6.             Users_score = Users_score + 1 'add one to the score'
    7.         Else
    8.             If (Answer.Text = "") Then
    9.                 MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
    10.                 Answer.SetFocus
    11.                 Exit Sub 'it will ignore all the code after this'
    12.                 End If
    13.         End If
    14.     End If
    15.     Unload Q1 'closes Question form'
    16.     Ans_1.Show 'loads Answer and comment form'
    17. End Sub

    well I don't get why you guys suggested to me to put Answer.SetFocus and Exit Sub on the following lines after the last message box?

    the funny thing is if I don't put them in, after I click OK in the message box that tells me that I have not entered anything, the program will go straight to Ans_1 form... why?

    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width