Currently my program fires out error messages when the user enters information outwith the boundaries. However for every valid entry these will be stored in the array and the entry outwith the boundaries will be listed as blank which I dont want. Id like the program to only store an entry in the array if all the criteria for the boundaries are met. Could anyone advise me how to do this?

Thanks

Dim Index As Integer = 0
    Dim EmployeeRec(9) As EmployeeType

    Private Sub add_bta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add_bta.Click

        If Val(employeeid_txt.Text) > 10 Or Val(employeeid_txt.Text) < 1 Then
            MessageBox.Show("Employee Id Outwith Range 1 to 10 ", "Please re-enter", MessageBoxButtons.OK)
        Else
            EmployeeRec(Index).Employeeid = employeeid_txt.Text
        End If

        EmployeeRec(Index).Status = "Active"

        If Len(firstname_txt.Text) > 15 Or Len(firstname_txt.Text) = 0 Then
            MessageBox.Show("Firstname Outwith Range 1 to 15 ", "Please re-enter", MessageBoxButtons.OK)
        Else
            EmployeeRec(Index).Firstname = firstname_txt.Text
        End If
        If Len(surname_txt.Text) > 15 Or Len(surname_txt.Text) = 0 Then
            MessageBox.Show("Surname Outwith Range 1 to 15 ", "Please re-enter", MessageBoxButtons.OK)
        Else
            EmployeeRec(Index).Surname = surname_txt.Text
        End If
        If Val(Age_txt.Text) > 100 Or Val(Age_txt.Text) < 16 Then
            MessageBox.Show("Age Outwith Range 16 to 100 ", "Please re-enter", MessageBoxButtons.OK)
        Else
            EmployeeRec(Index).Details.Age = Age_txt.Text
        End If
        If Len(gender_txt.Text) > 6 Or Len(gender_txt.Text) < 4 Then
            MessageBox.Show("Gender must be Male or Female ", "Please re-enter", MessageBoxButtons.OK)
        Else
            EmployeeRec(Index).Details.Gender = gender_txt.Text
        End If
        If Len(position_txt.Text) > 15 Or Len(position_txt.Text) = 0 Then
            MessageBox.Show("Position Outwith Range 1 to 15 ", "Please re-enter", MessageBoxButtons.OK)
        Else
            EmployeeRec(Index).Position = position_txt.Text
        End If

        Index = Index + 1

    End Sub

Recommended Answers

All 2 Replies

use boolean varaible to indicate if the user enter correct value for each input like this

If Val(employeeid_txt.Text) > 10 Or Val(employeeid_txt.Text) < 1 Then
            MessageBox.Show("Employee Id Outwith Range 1 to 10 ", "Please re-enter", MessageBoxButtons.OK)
        Else
           [B]correct_EmployeeID=true [/B]
         End If

then if all inputs for user are correct , store that entry to your array

if correct_EmployeeID and correct_firstname and correct_surname and correct_gender and correct_position then 
  EmployeeRec(Index).Employeeid = employeeid_txt.Text
etc ..etc
End if

other thing about your code , your validation based on test the length of the user's input
and that not correct , I can enter something like "abcd" and your program will accept it in the gender input , it is better to compare the user's input string with "male" or "female"
and much better to make user select the gender , and position (use radio button). about the error messages , what if user make more than one error ! many messages will show and that will be annoying , use labels to show the error messages

If Val(employeeid_txt.Text) > 10 Or Val(employeeid_txt.Text) < 1 Then
            lable_error_id.text="Employee Id Outwith Range 1 to 10 , Please re-enter"         Else
           correct_EmployeeID=true 
End If

Thanks Manal, thats helped a lot. Also your suggestion on better and more specific validation is insightful.

Cheers.

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.