简体   繁体   中英

VB.NET/Access - check if the first value of each row is less than a specific and then add text to each corresponding row in a datatable

I am trying to determine which students are eligible for promotion by checking how many classes they have attended against a known number of class and if they are add Eligible for promotion in the last value of each of the corresponding rows.

My end goal is to compare each persons total classes and check them against the requirements for each promotion. Each number below is the amount of time in grade at that level before they are eligible for promotion. I haven't tried getting the other belt levels yet. I have just been trying to get White Belt to Yellow Belt working.

  • Yellow Belt 12 Classes
  • Orange Belt 48 Classes
  • Green Belt 64 Classes
  • Purple Belt 144 Classes
  • Ikkyu 96 Classes
  • Nikyu 96 Classes
  • Sankyu 96 Classes
  • Shodan 192 Classes

数据网格视图的

Here is the code....

 ' RUN QUERY
    Access.ExecQuery("SELECT COUNT(StudentID) as TotalClasses, FirstName, LastName FROM Attend GROUP BY FirstName, LastName")

    ' REPORT & ABORT ON ERRORS
    If NoErrors(True) = False Then End



    Access.DBDT.Columns.Add("PromotionStatus", GetType(System.String))

    For Each row As DataRow In Access.DBDT.Rows
        For i = 0 To Access.DBDT.Rows.Count - 1
            If (row.Field(Of Integer)(0)) > 12 Then

                Access.DBDT.Rows(i)("PromotionStatus") = "Eligible for Yellow Belt"

            End If
        Next



    Next


    ' FILL DATAGRID
    dgvTestDue.DataSource = Access.DBDT

@JayV I tried the code you posted and it worked perfect for anyone with more than 12 classes. I then tried to build on that to process the rest of the time in grade/class requirement and it ignored everything after the first If and did not process any of the ElseIf.

Here is what I tried that didn't work....

 If (row.Field(Of Integer)("TotalClasses")) > 12 Then
            row("PromotionStatus") = "Eligible for Yellow Belt"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 48 Then
            row("PromotionStatus") = "Eligible for Yellow Belt"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 108 Then
            row("PromotionStatus") = "Eligible for Green Belt"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 252 Then
            row("PromotionStatus") = "Eligible for Purple Belt"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 348 Then
            row("PromotionStatus") = "Eligible for Ikkyu"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 444 Then
            row("PromotionStatus") = "Eligible for Nikyu"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 540 Then
            row("PromotionStatus") = "Eligible for Sankyu"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 636 Then
            row("PromotionStatus") = "Eligible for Shodan"
        ElseIf (row.Field(Of Integer)("TotalClasses")) > 780 Then
            row("PromotionStatus") = "Eligible for Nidan"
        End If

and this was the only way I got it to work. Is there any easier/more efficient way and why didn't the ElseIf work?

  If (row.Field(Of Integer)("TotalClasses")) > 12 Then
            row("PromotionStatus") = "Eligible for Yellow Belt"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 48 Then
            row("PromotionStatus") = "Eligible for Orange Belt"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 108 Then
            row("PromotionStatus") = "Eligible for Green Belt"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 252 Then
            row("PromotionStatus") = "Eligible for Purple Belt"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 348 Then
            row("PromotionStatus") = "Eligible for Ikkyu"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 444 Then
            row("PromotionStatus") = "Eligible for Nikyu"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 540 Then
            row("PromotionStatus") = "Eligible for Sankyu"
        End If

        If (row.Field(Of Integer)("TotalClasses")) > 636 Then
            row("PromotionStatus") = "Eligible for Shodan"
        End If

Sorry for the follow question after I marked the original post. I can ask this in a separate question if you want me to.

You are looping through the list of students twice. Once with

For Each row As DataRow In Access.DBDT.Rows

and a second time with

For i = 0 To Access.DBDT.Rows.Count - 1

and the final assignment of the Eligibility will be based on the last student in the list (Max Harris).

Using this should take care of the problem

For Each row As DataRow In Access.DBDT.Rows
    If (row.Field(Of Integer)("TotalClasses")) > 12 Then
        row("PromotionStatus") = "Eligible for Yellow Belt"
    End If
Next

It's generally better to use Column names rather than numbers as the order may change.

EDIT after supplemental:

When checking groups of numbers you need to check the upper and lower boundaries in each If (or ElseIf) statement.

Example:

Dim totalClasses As Integer = row.Field(Of Integer)("TotalClasses")
If totalClasses > 12 AndAlso totalClasses <= 48 Then
    row("PromotionStatus") = "Eligible for Yellow Belt"
ElseIf totalClasses > 48 AndAlso totalClasses <= 108 Then
    row("PromotionStatus") = "Eligible for Orange Belt"
ElseIf totalClasses > 108 AndAlso totalClasses <= 252 Then
    row("PromotionStatus") = "Eligible for Green Belt"
ElseIf totalClasses > 252 AndAlso totalClasses <= 348 Then
    row("PromotionStatus") = "Eligible for Purple Belt"
ElseIf totalClasses > 348 AndAlso totalClasses <= 444 Then
    row("PromotionStatus") = "Eligible for Ikkyu"
ElseIf totalClasses > 444 AndAlso totalClasses <= 540 Then
    row("PromotionStatus") = "Eligible for Nikyu"
ElseIf totalClasses > 540 AndAlso totalClasses <= 636 Then
    row("PromotionStatus") = "Eligible for Sankyu"
ElseIf totalClasses > 636 Then
    row("PromotionStatus") = "Eligible for Shodan"
End If

What was happening in your code was that the first If check was passing, therefore the rest of the cases were not checked against. It is a Logic Error in your code, not a syntax error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM