473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Dim names() As String = {"John", "Jake", "Jeff", "Juan", "Leo", _
  3.     "Sally", "Tom", "James", "Brandon", "Julius"}
  4.  
  5.     Dim amount(9) As Integer
  6.  
  7.     Sub getEachName()
  8.  
  9.         Dim intCount As Integer 'loop counter
  10.         'Add header to list box
  11.         lstTotals.Items.Add("Name and Hours Worked")
  12.  
  13.         'Get the total hours and put them in a list box with name
  14.  
  15.         For intCount = 0 To 9
  16.             Try
  17.                 amount(intCount) = CInt(InputBox("Enter amount of hours" _
  18.                 & vbLf & names(intCount)))
  19.                 lstTotals.Items.Add(names(intCount) & " " & amount(intCount))
  20.             Catch Exception As SystemException
  21.                 MessageBox.Show("Please enter a numeric value.")
  22.  
  23. ------> ????????????????
  24.  
  25.             End Try
  26.         Next intCount
  27.  
  28.     End Sub
  29.  
Mar 28 '08 #1
4 3431
Try this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     Dim names(9) As String
  4.  
  5.     Dim amount(9) As Integer
  6.  
  7.     names(0) = "John"
  8.     names(1) = "Jake"
  9.     names(2) = "Jeff"
  10.     names(3) = "Juan"
  11.     names(4) = "Leo"
  12.     names(5) = "Sally"
  13.     names(6) = "Tom"
  14.     names(7) = "James"
  15.     names(8) = "Brandon"
  16.     names(9) = "Julius"
  17.  
  18.     Dim intCount As Integer 'loop counter
  19.  
  20.     Dim TempNum As String
  21.  
  22.     'Add header to list box
  23.     lstTotals.AddItem "Name and Hours Worked"
  24.  
  25.     'Get the total hours and put them in a list box with name
  26.     For intCount = 0 To 9
  27.         TempStr = InputBox("Enter amount of hours" & vbLf & names(intCount))
  28.         If TempStr = vbNullString Then
  29.             Exit For
  30.         ElseIf Not IsNumeric(TempStr) Then
  31.             MsgBox ("Please enter a numeric value.")
  32.             intCount = intCount - 1
  33.         Else
  34.             amount(intCount) = CInt(TempStr)
  35.             lstTotals.AddItem names(intCount) & " " & amount(intCount)
  36.         End If
  37.     Next intCount
  38.  
  39. End Sub
  40.  
This code was adapted for Visual Basic 6, but should still work in VB.NET.
Mar 28 '08 #2
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?

Expand|Select|Wrap|Line Numbers
  1.     Sub fewesthours()
  2.         Dim intCount As Integer
  3.         Dim intMinimum As Integer = hours(0)
  4.         Dim strName As String
  5.  
  6.         For intCount = 0 To (hours.Length - 1)
  7.             If hours(intCount) < intMinimum Then
  8.                 intMinimum = hours(intCount)
  9.                 strName = names(intCount).ToString()
  10.             End If
  11.         Next intCount
  12.  
  13.         lblfewhours.Text = "Fewest Hours Worked:" & strName & intMinimum
  14.  
  15.     End Sub
  16.  
Mar 29 '08 #3
QVeen72
1,445 Expert 1GB
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..

Expand|Select|Wrap|Line Numbers
  1.     Sub fewesthours()
  2.         Dim intCount As Integer
  3.         Dim intMinimum As Integer = hours(0)
  4.         Dim strName As String = names(0).ToString()
  5.  
  6.         For intCount = 0 To (hours.Length - 1)
  7.             If hours(intCount) < intMinimum Then
  8.                 intMinimum = hours(intCount)
  9.                 strName = names(intCount).ToString()
  10.             End If
  11.         Next intCount
  12.  
  13.         lblfewhours.Text = "Fewest Hours Worked:" & strName & intMinimum
  14.  
  15.     End Sub
  16.  
Regards
Veena
Mar 30 '08 #4
wow... lol thanks qveen. ;)
Mar 30 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: The Plankmeister | last post by:
Hi... What's the best method of validating input characters? I would like to prevent users submitting exotic characters (such as those acquired on Windows Systems by pressing ALT+) and thought...
4
by: Gleep | last post by:
Hi PHP coders, I've got an issue I'm stuck with. Imagine there is a large form that has 5 columns and 20 rows. In each row there is a check box - then 4 input fields. I already have the code...
3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
2
by: Lüpher Cypher | last post by:
Hi, I have a form: <form .. onSubmit="return onFormSubmit(this)"> <input type="checkbox" name="a" value="1" /> <input type="checkbox" name="a" value="2" /> ... <input type="checkbox" name="a"...
1
by: platostoteles | last post by:
Hallo NG, I am new to JavaScript and would really appreciate any help to solve my problem. I am using the blow code in my form to validate form fields. What I would like to accomplish is that...
17
by: stathis gotsis | last post by:
Hello everyone, I am tying to come up with an elegant way to process some input data that come from a form. When the user hits the 'Submit' button, i want the form to appear again with the...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
6
by: Advo | last post by:
Hi there. Having a few issues and cannot see a way to actually do the following. ive duplicated a simple questionnaire as shown here: http://www.hackeradio.com/questionnaire.html meaning...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.