Okay, first I must say I am new to this but I am a quick study. I have a project that works as coded except for in one instance of validation. I am accepting numeric input from the user and anytime the input is not numeric or no entry is given, I have coded a messagebox error within a Do-Loop to once again prompt for the correct input.

The problem is that the code works fine when any numbers are out of range and will display the proper messagebox error BUT when anything else like letters or no data is entered the code crashes during my validation. I have placed the code with validations below. Please tell me why the If statement in the Do-Loop seems to be failing. Thanks in advance for the help all.

The Freaky One

<Code Follows>

'Constant Variables
Dim Student As String
Dim Gr1 As Single
Dim Gr2 As Single
Dim Gr3 As Single
Dim Average As Single
Dim Grade As String

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'End Program
End
End Sub

Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click

'Gather information
Do
Student = InputBox("Please enter the Student's Name:", "Student Name")
Loop While Student = ""

Do
Gr1 = InputBox("Please enter " & Student & "'s first test score:", "First Test Score")
If Not IsNumeric(Gr1) Or 100 >= Gr1 >= 0 Then
MessageBox.Show("Please enter a valid first test score.")
End If
Loop Until IsNumeric(Gr1) And Gr1 >= 0

Do
Gr2 = InputBox("Please enter " & Student & "'s second test score:", "First Test Score")
If Not IsNumeric(Gr2) Or 100 >= Gr2 >= 0 Then
MessageBox.Show("Please enter a valid second test score.")
End If
Loop Until IsNumeric(Gr2) And Gr2 >= 0

Do
Gr3 = InputBox("Please enter " & Student & "'s third test score:", "First Test Score")
If Not IsNumeric(Gr3) Or 100 >= Gr3 >= 0 Then
MessageBox.Show("Please enter a valid third test score.")
End If
Loop Until IsNumeric(Gr3) And Gr3 >= 0

'Calculate and Display Data
Average = (Gr1 + Gr2 + Gr3) / 3
If Average >= 90 Then
Grade = "A"
ElseIf Average >= 80 Then
Grade = "B"
ElseIf Average >= 70 Then
Grade = "C"
ElseIf Average >= 60 Then
Grade = "D"
Else : Grade = "F"
End If

lstTally.Items.Add(Student & " Average: " & Average.ToString & " Grade: " & Grade)

End Sub
End Class

</Code Ends>

The program runs normally and successfully when proper data is entered. Validation works properly for the name. Only the numeric responses are crashing out when non numeric input is entered.

Recommended Answers

All 2 Replies

hi TurboFreak
you have two problems
First :-

Gr1 = InputBox("Please enter " & Student & "'s first test score:", "First Test Score")
while you have defined Gr1 as single if user entered any thing rather than numbers the program crash.....so first thing define Gr1 as string

Second
If Not IsNumeric(Gr1) Or 100 >= Gr1 >= 0 Then

if you defined Gr1 as string this if statment will not work
coz Gr1 is string and cant compare it with numbers so compare it just when its number

so the do loop will be like this

Do
Gr1 = InputBox("Please enter " & Student & "'s first test score:", "First Test Score")

If Not IsNumeric(Gr1) Then
MessageBox.Show("Please enter a valid first test score.")
Else
s1 = CSng(Gr1)
If s1 > 100 Or s1 < 0 Then

MessageBox.Show("Please enter a valid first test score.")

End If
End If

Loop Until IsNumeric(Gr1) And s1 >= 0 and s1 <= 100


note : the avrage will be
Average = (s1 + s2 + s3) / 3
where s1 and s2 and s3 are single

i hope this will solve the problem

Awesome!! Thank you thank you thank you. It is so obvious now that I see it. Yes you are correct. When I tried to change the variable to a string the number validation failed. I had tried everything except for making it two separate validation checks. You rock Brother and thank you. I beat my head out on that one for awhile. Even rewrote it just in the event I forgot something small. Thanks again for the help as it is greatly apreciated!!!

TurboFreak:mrgreen::p:lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.