Just trying to learn validation

jenok2000

Member
Joined
Apr 9, 2006
Messages
6
Programming Experience
Beginner
Hello!! VB.NET 2003
I am so new at this it isn't funny. I am right now programming a fun little test that people take in order to find out their life expectancy. Any how it is a 3 page application and and before the user clicks the "Next Page" button I want the program to catch any questions that haven't been answered.

The questions are asked and then the user fills in 1 of 2 radio buttons that are grouped together by the panel control.

What I want is instead of having multiple boxes pop up when the user forgets to answer more than one question, I want only one box that states "You have not answered Question#: 1, 5, and 10. You need to answer these before moving on to the next page" And I don't want the user to be able to go to the next page until these fields have been filled in. Down below I will put what I have if someone could help me out I would be so greatful:) Thanks!!! PS... as it stands my code won't even let me get to the second page anyways....This can be awfully frustrating:confused:


Private
Sub btnNext_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnNext.Click
If mypart1.rdoYes01.Checked = FalseAnd mypart1.rdoNo01.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 1!")
EndIf
If mypart1.rdoYes02.Checked = FalseAnd mypart1.rdoNo02.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 2!")
EndIf
If mypart1.rdoYes03.Checked = FalseAnd mypart1.rdoNo03.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 3!")
EndIf
If mypart1.rdoYes04.Checked = FalseAnd mypart1.rdoNo04.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 4!")
EndIf
If mypart1.rdoYes05.Checked = FalseAnd mypart1.rdoNo05.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 5!")
EndIf
If mypart1.rdoYes06.Checked = FalseAnd mypart1.rdoNo06.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 6!")
EndIf
If mypart1.rdoYes07.Checked = FalseAnd mypart1.rdoNo07.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 7!")
EndIf
If mypart1.rdoYes08.Checked = FalseAnd mypart1.rdoNo08.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 8!")
EndIf
If mypart1.rdoYes09.Checked = FalseAnd mypart1.rdoNo09.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 9!")
EndIf
If mypart1.rdoYes10.Checked = FalseAnd mypart1.rdoNo10.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 10!")
EndIf
ExitSub
mypart2 = New part2
mypart2.Show()
mypart1.Hide()
EndSub
 
Last edited:
jenok2000 said:
Hello!! VB.NET 2003
I am so new at this it isn't funny. I am right now programming a fun little test that people take in order to find out their life expectancy. Any how it is a 3 page application and and before the user clicks the "Next Page" button I want the program to catch any questions that haven't been answered.

The questions are asked and then the user fills in 1 of 2 radio buttons that are grouped together by the panel control.

What I want is instead of having multiple boxes pop up when the user forgets to answer more than one question, I want only one box that states "You have not answered Question#: 1, 5, and 10. You need to answer these before moving on to the next page" And I don't want the user to be able to go to the next page until these fields have been filled in. Down below I will put what I have if someone could help me out I would be so greatful:) Thanks!!! PS... as it stands my code won't even let me get to the second page anyways....This can be awfully frustrating:confused:


Private
Sub btnNext_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnNext.Click
If mypart1.rdoYes01.Checked = FalseAnd mypart1.rdoNo01.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 1!")
EndIf
If mypart1.rdoYes02.Checked = FalseAnd mypart1.rdoNo02.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 2!")
EndIf
If mypart1.rdoYes03.Checked = FalseAnd mypart1.rdoNo03.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 3!")
EndIf
If mypart1.rdoYes04.Checked = FalseAnd mypart1.rdoNo04.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 4!")
EndIf
If mypart1.rdoYes05.Checked = FalseAnd mypart1.rdoNo05.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 5!")
EndIf
If mypart1.rdoYes06.Checked = FalseAnd mypart1.rdoNo06.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 6!")
EndIf
If mypart1.rdoYes07.Checked = FalseAnd mypart1.rdoNo07.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 7!")
EndIf
If mypart1.rdoYes08.Checked = FalseAnd mypart1.rdoNo08.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 8!")
EndIf
If mypart1.rdoYes09.Checked = FalseAnd mypart1.rdoNo09.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 9!")
EndIf
If mypart1.rdoYes10.Checked = FalseAnd mypart1.rdoNo10.Checked = FalseThen
MessageBox.Show("Hey!! You need to answer number 10!")
EndIf
ExitSub
mypart2 = New part2
mypart2.Show()
mypart1.Hide()
EndSub

What i will do is create a String to carry the number of question user not yet answer then display in one time.

Dim sQuestion AS String = ""
PrivateSub btnNext_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnNext.Click
If mypart1.rdoYes01.Checked = FalseAnd mypart1.rdoNo01.Checked = FalseThen
sQuestion += "1, "

EndIf
If mypart1.rdoYes02.Checked = FalseAnd mypart1.rdoNo02.Checked = FalseThen
sQuestion += "2, "
EndIf
If mypart1.rdoYes03.Checked = FalseAnd mypart1.rdoNo03.Checked = FalseThen
sQuestion += "3, "
EndIf
If mypart1.rdoYes04.Checked = FalseAnd mypart1.rdoNo04.Checked = FalseThen
sQuestion += "4, "
EndIf
If mypart1.rdoYes05.Checked = FalseAnd mypart1.rdoNo05.Checked = FalseThen
sQuestion += "5, "
EndIf
If mypart1.rdoYes06.Checked = FalseAnd mypart1.rdoNo06.Checked = FalseThen
sQuestion += "6, "
EndIf
If mypart1.rdoYes07.Checked = FalseAnd mypart1.rdoNo07.Checked = FalseThen
sQuestion += "7, "
EndIf
If mypart1.rdoYes08.Checked = FalseAnd mypart1.rdoNo08.Checked = FalseThen
sQuestion += "8, "
EndIf
If mypart1.rdoYes09.Checked = FalseAnd mypart1.rdoNo09.Checked = FalseThen
sQuestion += "9, "
EndIf
If mypart1.rdoYes10.Checked = FalseAnd mypart1.rdoNo10.Checked = FalseThen
sQuestion += "10, "
EndIf

MessageBox.Show("You have not answered Question#: " & sQuestion & "You need to answer these before moving on to the next page")
 
You can go one sep beyond and keep track of the number of skipped questions:


PrivateSub btnNext_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnNext.Click
Dim sQuestion AS String = ""
Dim Count AS Integer = 0
If mypart1.rdoYes01.Checked = FalseAnd mypart1.rdoNo01.Checked = FalseThen
sQuestion += "1, "
Count += 1
EndIf
If mypart1.rdoYes02.Checked = FalseAnd mypart1.rdoNo02.Checked = FalseThen
sQuestion += "2, "
Count += 1
EndIf
If mypart1.rdoYes03.Checked = FalseAnd mypart1.rdoNo03.Checked = FalseThen
sQuestion += "3, "
Count += 1
EndIf
If mypart1.rdoYes04.Checked = FalseAnd mypart1.rdoNo04.Checked = FalseThen
sQuestion += "4, "
Count += 1
EndIf
If mypart1.rdoYes05.Checked = FalseAnd mypart1.rdoNo05.Checked = FalseThen
sQuestion += "5, "
Count += 1
EndIf
If mypart1.rdoYes06.Checked = FalseAnd mypart1.rdoNo06.Checked = FalseThen
sQuestion += "6, "
Count += 1
EndIf
If mypart1.rdoYes07.Checked = FalseAnd mypart1.rdoNo07.Checked = FalseThen
sQuestion += "7, "
Count += 1
EndIf
If mypart1.rdoYes08.Checked = FalseAnd mypart1.rdoNo08.Checked = FalseThen
sQuestion += "8, "
Count += 1
EndIf
If mypart1.rdoYes09.Checked = FalseAnd mypart1.rdoNo09.Checked = FalseThen
sQuestion += "9, "
Count += 1
EndIf
If mypart1.rdoYes10.Checked = FalseAnd mypart1.rdoNo10.Checked = FalseThen
sQuestion += "10, "
Count += 1
EndIf

If count = 1 Then
MessageBox.Show("You have not answered Question#: " & sQuestion & "You need to answer before moving on to the next page")
ElseIf count > 1 Then
MessageBox.Show("You have not answered Questions #: " & sQuestion & "You need to answer all questions moving on to the next page")
End If
 
Wow That is cool, I tried it out. Thank You so much!! It is so much fun learning new things that you can't get out of a beginners book/class. The only problem that I am having with the code I organially had done was the

Exit Sub
mypart2 = New part2
mypart2.Show()
mypart1.Hide()

I need to stop the button from taking it to the next page until all the questions are complete, but even when they are complete, it won't let me move on to the second part. I know it is something simple with that, and I read 4 articles pertaing to hiding and showing forms, which is where I got this from, but the articles didn't cover validation.
 
Code the new page call inside a conditional statement.

VB.NET:
        Select Case count
            Case Is = 0
                mypart2 = New part2
                mypart2.Show()
                mypart1.Hide()
            Case Is = 1
                MessageBox.Show("You have not answered Question#: " & squestion & "You need to answer before moving on to the next page")
            Case Else
                MessageBox.Show("You have not answered Questions #: " & squestion & "You need to answer all questions moving on to the next page")
        End Select
 
NEVER MIND I DID IT!!!!! :) Thank you so much!!! I had it the wrong form, sometimes if I go to long at this I begin to get sloppy. My head tends to swim. Thank you again.
 
Hey one last question about this.......

What I was wondering was, why do I have to use the end select in this button vs. the next buttons.......

But far more importantly, I wanted to know how I could code the valadation if I have question 21 disabled. If the user clicks that they are a male, I have this code in my module form and I call it in the load event of my third form, (where a girl question is asked which happens to be q 21).

PublicSub male()
If mypart1.rdoYes01.Checked = TrueThen mypart3.rdoYes21.Enabled = False
EndSub


But when it comes to validation, it wants the user to answer the question, even though it is "disabled" and the user can't because it is greyed out.

sooooo.... What can I do to disable the question in the validation. I have played and played with it with out luck...any ideas???

Here is what I have in my "Calculate My Life Expectancy" button:

If
rdoYes21.Checked = FalseAnd rdoNo21.Checked = FalseThen
MessageBox.Show("Question 21 was not answered")
ExitSub
EndIf
If rdoYes22.Checked = FalseAnd rdoNo22.Checked = FalseThen
MessageBox.Show("Question 22 was not answered")
ExitSub
EndIf
If rdoYes23.Checked = FalseAnd rdoNo23.Checked = FalseThen
MessageBox.Show("Question 23 was not answered")
ExitSub
EndIf
If rdoYes24.Checked = FalseAnd rdoNo24.Checked = FalseThen
MessageBox.Show("Question 24 was not answered")
ExitSub
EndIf
If rdoYes25.Checked = FalseAnd rdoNo25.Checked = FalseThen
MessageBox.Show("Question 25 was not answered")
ExitSub
EndIf
If rdoExc5.Checked = FalseAnd rdoExc2.Checked = FalseAnd RdoExcNone.Checked = FalseThen
MessageBox.Show("Question 26 was not answered")
ExitSub
EndIf
If rdoCig2.Checked = FalseAnd rdoCig1.Checked = FalseAnd rdoCigLess.Checked = FalseAnd RdoCigNone.Checked = FalseThen
MessageBox.Show("Question 27 was not answered")
ExitSub
EndIf
If rdoWgt50.Checked = FalseAnd rdoWgt30.Checked = FalseAnd rdoWgt10.Checked = FalseAnd rdowgtnone.Checked = FalseThen
MessageBox.Show("Question 28 was not answered")
ExitSub
EndIf
If rdoHappy.Checked = FalseAnd rdoUnhappy.Checked = FalseThen
MessageBox.Show("Question 29 was not answered")
ExitSub
EndIf

Thanks
 
VB.NET:
        If rdoYes21.Checked = False And rdoNo21.Checked = False [COLOR=Blue]And rdoYes21.Enabled = True[/COLOR] Then
            MessageBox.Show("Question 21 was not answered")
            ExitSub()
        End If

P.S. I'm not a fan of the 'ExitSub' you are using. It violates a guideline for structured programming - single entry and single exit from a module. You could look into nested 'IF' statements, but that would get messy.

If you don't care, you can certainly leave it the way it is.
 
Last edited:
Firstly, for each msgbox being shown one after another, why not do like you did with the previous page and build a string and then use that.

Secondly, whats wrong with using Exit Sub? All it does is break from the current code being executed which is exactly the same thing as breaking from a while, for or repeat loop. That would mean that using these kinds of loops goes against structured programming as well *scratch* but I guess structured doesnt have flexible connotations - oh well
 
What's wrong with using Exit Sub? Have you ever tried to flow chart it? Using Exit Sub is exactly the same thing as using the old GOTO statements. Structured programming was established to help do away with the GOTO statement and help make very long strings of code more readable and easier to update. As for which control structures are 'allowed' you can read the link below, this is not the place for Structured Programming 101. Breaking from a loop is certainly acceptable, when the condition of the loop has been met (or broken) - that is your single exit from the loop.
Control Structures within Programs
 
Back
Top