|
-
Jul 29th, 2003, 02:43 AM
#1
Thread Starter
Junior Member
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:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
If (Answer.Text = "water") Then 'If correct answer then'
Users_score = Users_score + 1 'add one to the score'
End If
Unload Q1 'closes Question 1 form'
Ans_1.Show 'loads Answer and comment form for question 1'
End If
End Sub
'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:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
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.'
Users_score = Users_score + 1
End If
Unload Q4 'closes Question 4 form'
Ans_4.Show 'loads Answer and comment form for question 4'
End If
End Sub
'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:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
Users_score = Users_score + 1 'add one to the score'
Unload Q6 'closes Question 1 form'
Finish_page.Show 'loads Answer and comment form for question 1'
End If
End Sub
'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.'
-
Jul 29th, 2003, 02:51 AM
#2
Addicted Member
hi 
try:
Private Sub Continue_Click()
if Answer.Text = "" then
msgbox "Please type your answer here"
Answer.Setfocus
exit sub
end if
end sub
-
Jul 29th, 2003, 03:52 AM
#3
Addicted Member
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:
copy into a bas module:
Public Function IsTextEmpty(ByVal strText As String) As Boolean
IsTextEmpty = CBool(Len(Trim$(strText)) = 0)
End Function
To use in a form:
If IsTextEmpty(Textbox1.Text) then
Msgbox "No data entered"
End if
Cheers
Jack
-
Jul 30th, 2003, 01:27 AM
#4
Thread Starter
Junior Member
could there be a more simple way?
btw, thx jewel but it didn't work...
-
Jul 30th, 2003, 01:59 AM
#5
Thread Starter
Junior Member
Okay I did what Jewel said but it didin't work...
VB Code:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
If Answer.Text = "" Then
MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
Else
If (Answer.Text = "bread") Then 'If correct answer then'
Users_score = Users_score + 1 'add one to the score'
End If
End If
End If
Unload Q2 'closes Question 1 form'
Ans_2.Show 'loads Answer and comment form for question 1'
End Sub
-
Jul 30th, 2003, 02:21 AM
#6
Addicted Member
Okay I did what Jewel said but it didin't work...
VB Code:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
If Answer.Text = "" Then
MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
Else
If (Answer.Text = "bread") Then 'If correct answer then'
Users_score = Users_score + 1 'add one to the score'
End If
End If
End If
Unload Q2 'closes Question 1 form'
Ans_2.Show 'loads Answer and comment form for question 1'
End Sub
The first code I sent you works fine for me maybe we can revised your code to this:
VB Code:
Private Sub Continue_Click()
'------------------------------
' validate first input
If Answer.Text = "" Then
MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
Answer.SetFocus
Exit Sub
End If
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Answer.SetFocus
Exit Sub
End If
'---------------------------------------------
If (Answer.Text = "bread") Then 'If correct answer then'
Users_score = Users_score + 1 'add one to the score'
End If
Unload Q2 'closes Question 1 form'
Ans_2.Show 'loads Answer and comment form for question 1'
End Sub
or you can make a function returning true if the input is valid like Jackalx25 did
VB Code:
Private Sub Continue_Click()
If IsValid(Answer.Text) = True Then
If (Answer.Text = "bread") Then 'If correct answer then'
Users_score = Users_score + 1 'add one to the score'
End If
Else
MsgBox "Invalid Answer!!"
Answer.SetFocus
End If
Unload Q2 'closes Question 1 form'
Ans_2.Show 'loads Answer and comment form for question 1'
End Sub
Private Function IsValid(str As String) As Boolean
IsValid = True
If Answer.Text = "" Or IsNumeric(Answer.Text) = True Then
IsValid = False
End If
End Function
-
Jul 30th, 2003, 02:22 AM
#7
Thread Starter
Junior Member
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?
-
Jul 30th, 2003, 02:41 AM
#8
Addicted Member
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:
If Answer.Text = "" then
msgbox "Please Enter something"
Answer.Setfocus
[B]Exit Sub[/B] 'it will ignore all the code after this
end if
hope it wors
-
Jul 30th, 2003, 03:04 AM
#9
Thread Starter
Junior Member
well other questions forms worked exept this one and the coding is
VB Code:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
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.'
Users_score = Users_score + 1
Else
If (Answer.Text = "") Then
MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
Answer.SetFocus
Exit Sub 'it will ignore all the code after this'
End If
Unload Q4 'closes Question form'
Ans_4.Show 'loads Answer and comment form'
End If
End If
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?
-
Jul 30th, 2003, 03:09 AM
#10
Thread Starter
Junior Member
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
-
Jul 30th, 2003, 03:16 AM
#11
Addicted Member
well, i run your program and this is what i found out
your condition
VB Code:
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:
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:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
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.'
Users_score = Users_score + 1
Else
If (Answer.Text = "") Then
MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
Answer.SetFocus
Exit Sub 'it will ignore all the code after this'
End If
Unload Q4 'closes Question form'
Ans_4.Show 'loads Answer and comment form'
End If
End If
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?
-
Jul 30th, 2003, 03:21 AM
#12
Addicted Member
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!!!
-
Jul 30th, 2003, 06:30 PM
#13
Thread Starter
Junior Member
okay I need some explaination...
VB Code:
Private Sub Continue_Click()
If IsNumeric(Answer.Text) = True Then 'check for correct data type'
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'
Else
If (Answer.Text = "water") Then 'If correct answer then'
Users_score = Users_score + 1 'add one to the score'
Else
If (Answer.Text = "") Then
MsgBox "You havn't answered yet!", 1 + 48, "Come on..."
Answer.SetFocus
Exit Sub 'it will ignore all the code after this'
End If
End If
End If
Unload Q1 'closes Question form'
Ans_1.Show 'loads Answer and comment form'
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|