Validating input from an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tragic54
    New Member
    • Feb 2008
    • 20

    Validating input from an array

    Ok so i'm trying to validate numeric input through the use of input boxes. I'm trying to use a try catch statement to catch anything thats non numeric and it works fine. But the only problem i'm having is looping through the array. For example an input box pops up asking John for hours worked, enters 20 and clicks ok. Then loops to the next inputbox asking Jake for hours worked, enters 30 clicks ok. Now when the next question comes up for 'Jeff' If he clicks 'ok' 'cancel' or enters a non integer it catches it, displays 'Please enter a numeric value' then goes to the next person. Once it catches an error I need the loop to reask that same question to the same person before proceeding to the next. Anyone give me any tips? Theres a lot more code to it, but i just included the arrays and the subprocedure to get the hours worked.

    Code:
       
     Dim names() As String = {"John", "Jake", "Jeff", "Juan", "Leo", _
        "Sally", "Tom", "James", "Brandon", "Julius"}
    
        Dim amount(9) As Integer
    
        Sub getEachName()
    
            Dim intCount As Integer 'loop counter
            'Add header to list box
            lstTotals.Items.Add("Name and Hours Worked")
    
            'Get the total hours and put them in a list box with name
    
            For intCount = 0 To 9
                Try
                    amount(intCount) = CInt(InputBox("Enter amount of hours" _
                    & vbLf & names(intCount)))
                    lstTotals.Items.Add(names(intCount) & " " & amount(intCount))
                Catch Exception As SystemException
                    MessageBox.Show("Please enter a numeric value.")
    
    ------> ????????????????
    
                End Try
            Next intCount
    
        End Sub
    Last edited by tragic54; Mar 28 '08, 01:57 AM. Reason: typo
  • tristanlbailey
    New Member
    • Apr 2007
    • 30

    #2
    Try this:

    Code:
    Private Sub Form_Load()
    
        Dim names(9) As String
        
        Dim amount(9) As Integer
        
        names(0) = "John"
        names(1) = "Jake"
        names(2) = "Jeff"
        names(3) = "Juan"
        names(4) = "Leo"
        names(5) = "Sally"
        names(6) = "Tom"
        names(7) = "James"
        names(8) = "Brandon"
        names(9) = "Julius"
        
        Dim intCount As Integer 'loop counter
        
        Dim TempNum As String
        
        'Add header to list box
        lstTotals.AddItem "Name and Hours Worked"
        
        'Get the total hours and put them in a list box with name
        For intCount = 0 To 9
            TempStr = InputBox("Enter amount of hours" & vbLf & names(intCount))
            If TempStr = vbNullString Then
                Exit For
            ElseIf Not IsNumeric(TempStr) Then
                MsgBox ("Please enter a numeric value.")
                intCount = intCount - 1
            Else
                amount(intCount) = CInt(TempStr)
                lstTotals.AddItem names(intCount) & " " & amount(intCount)
            End If
        Next intCount
    
    End Sub
    This code was adapted for Visual Basic 6, but should still work in VB.NET.

    Comment

    • tragic54
      New Member
      • Feb 2008
      • 20

      #3
      Alright so with a few changes i got that part working. Now my next problem i've come across. I got the highest hours worked to display the persons name and hours worked in the label. Now when i'm trying to do it for fewest hours worked, the name of the person with the fewest hours wont display. Anyone point me in the right direction?

      Code:
          Sub fewesthours()
              Dim intCount As Integer
              Dim intMinimum As Integer = hours(0)
              Dim strName As String
      
              For intCount = 0 To (hours.Length - 1)
                  If hours(intCount) < intMinimum Then
                      intMinimum = hours(intCount)
                      strName = names(intCount).ToString()
                  End If
              Next intCount
      
              lblfewhours.Text = "Fewest Hours Worked:" & strName & intMinimum
      
          End Sub

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        You have not assigned strName with the Names(0)...
        You code will not work if the first array element is Least..
        anyway, check this modified code..

        [code=vb]
        Sub fewesthours()
        Dim intCount As Integer
        Dim intMinimum As Integer = hours(0)
        Dim strName As String = names(0).ToStri ng()

        For intCount = 0 To (hours.Length - 1)
        If hours(intCount) < intMinimum Then
        intMinimum = hours(intCount)
        strName = names(intCount) .ToString()
        End If
        Next intCount

        lblfewhours.Tex t = "Fewest Hours Worked:" & strName & intMinimum

        End Sub
        [/code]

        Regards
        Veena

        Comment

        • tragic54
          New Member
          • Feb 2008
          • 20

          #5
          wow... lol thanks qveen. ;)

          Comment

          Working...