input validation

i am trying to put an input validation in a program to only allow the

Page 1 of 1

3 Replies - 2386 Views - Last Post: 07 December 2008 - 10:52 AM Rate Topic: -----

#1 Shelly1976  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-October 08

input validation

Post icon  Posted 05 December 2008 - 08:08 PM

nothing i do seems to work can anyone help here is my code so far


  'Declare variables 

	Dim intRooms As Integer
	Dim intOccupied As Integer
	Dim x As Integer
	Const DecFLOORS As Integer = 30
	Const TotalRooms = 240
	Dim strOut As String




	Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplete.Click

		'initialize loop

		For x = 1 To 8

			'Input number of rooms used per floor

			intOccupied = InputBox("How many rooms were occupied on floor " & x)


			If intOccupied < 1 Or intOccupied > 30 Then
			Else
				MessageBox.Show("Number of rooms occupied must be between 1 and 30")
			End If


			lstbx.Items.Add("Floor:  " & x & " " & "Rooms Occupied: " & " " & intOccupied & " Occupancy Rate:  " & FormatPercent(intOccupied / (TotalRooms / 8)))
			intRooms = intRooms + intOccupied

		Next

		'display the total number of rooms occupied

		lblTotalOccupied.Text = intRooms

		'display the overall occupancy rate for all floors

		lblOccupancyRate.Text = FormatPercent(intRooms / TotalRooms)

		'Add the output string to the list box

		lstbx.Text = strOut



	End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: input validation

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: input validation

Posted 05 December 2008 - 11:15 PM

InputBox returns a string, not an integer. I add a "Val(strOut)" to get the integer.

I put a do..loop around the inputbox processing, so it will loop if the wrong values are entered.

Finally, i commented out lstbx.Text = strOut since strOut is never updated.

You probably want to clear the labels and list box when the subroutine is entered.

	Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click

//		'Declare variables 
		Const DecFLOORS As Integer = 30
		Const TotalRooms = 240
		Dim intRooms As Integer
		Dim intOccupied As Integer
		Dim strOut As String
		Dim x As Integer

//		'initialize loop

		For x = 1 To 8

//			'Input number of rooms used per floor

			Do
				strOut = InputBox("How many rooms were occupied on floor " & x)

				If strOut.Length() = 0 Then
					Exit Sub

				End If

				intOccupied = Val(strOut)

				If intOccupied < 1 Or intOccupied > 30 Then
					MessageBox.Show("Number of rooms occupied must be between 1 and 30")
				Else
					Exit Do

				End If
			Loop

			lstbx.Items.Add("Floor:  " & x & " " & "Rooms Occupied: " & " " & intOccupied & " Occupancy Rate:  " & FormatPercent(intOccupied / (TotalRooms / 8)))

			intRooms = intRooms + intOccupied

		Next

//		'display the total number of rooms occupied

		lblTotalOccupied.Text = intRooms

//		'display the overall occupancy rate for all floors

		lblOccupancyRate.Text = FormatPercent(intRooms / TotalRooms)

//		'Add the output string to the list box

//		'lstbx.Text = strOut

	End Sub


Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: input validation

Posted 05 December 2008 - 11:18 PM

First of all, declare your variables that are going to use in the button event, not outside it. This makes them at a scope that doesn't work and may cause problems, especially for your use of "x".

Secondly, you have a logic problem with your if statement...

If intOccupied < 1 Or intOccupied > 30 Then
Else
				MessageBox.Show("Number of rooms occupied must be between 1 and 30")
End If



You are saying here if the number is less than 1 or greater than 20 then it is ok, otherwise make the error message. I think you have this backwards if you want to have between 1 and 30...


If intOccupied >= 1 And intOccupied <= 30 Then
Else
				MessageBox.Show("Number of rooms occupied must be between 1 and 30")
End If

' Or better yet still... Notice same as you have but no else statement.

If intOccupied < 1 or intOccupied > 30 Then
	 MessageBox.Show("Number of rooms occupied must be between 1 and 30")
End If



In either case above now the message will appear any time you put in a number that is not within the range of 1 - 30 (inclusive).

The last thing to watch out for with the inputBox function is that the user can enter a letter, a sequence of letters, numbers and punctuation and most importantly hit the cancel button. Typing any of these as well as hitting the cancel will cause an error. So be sure to check for empty strings that are returned when they press cancel etc.

Enjoy! :)
Was This Post Helpful? 1
  • +
  • -

#4 Shelly1976  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-October 08

Re: input validation

Posted 07 December 2008 - 10:52 AM

ok that worked great for that program thanks but the next program im working on the professor wants us to use the input validation we are restricting it so that you can't enter a negative value i got the program to work using message boxes but he wants us to use the input validation instead this is what my code looks like any help would be greatly appriciated

 Dim DecTotalCharges As Decimal
	Dim intMeds As Integer
	Dim intSurgery As Integer
	Dim intLab As Integer
	Dim intRehab As Integer

	Function StayCharges() As Decimal

		'Calculate hospital stay charges

		Dim intStay As Integer
		intStay = CInt(tbxdays.Text)
		If intStay < 0 Then
			MessageBox.Show("Enter a number 0 or greater for Length of Stay ")
		End If
		If intStay > 0 Then
			Return CDec(intStay * 350)
		Else
			Return 0D
		End If


	End Function
	Function MisCharges() As Decimal

		'Calculate other charges

		

		intMeds = CInt(tbxMeds.Text)
		If intMeds < 0 Then
			MessageBox.Show("Enter a number 0 or greater for Medication Charges ")
		End If
		intSurgery = CInt(tbxSurgical.Text)
		If intSurgery < 0 Then
			MessageBox.Show("Enter a number 0 or greater for Surgical Charges")
		End If
		intLab = CSng(tbxLab.Text)
		If intLab < 0 Then
			MessageBox.Show("Enter a number 0 or greater for Lab Charges")
		End If
		intRehab = CInt(tbxRehab.Text)
		If intRehab < 0 Then
			MessageBox.Show("Enter a number 0 or greater Physical Rehabilitation")
		End If

		Return CDec(intMeds + intSurgery + intLab + intRehab)

	End Function

	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

		'Calculate total charges

		Dim DecTotalCharges As Decimal
		DecTotalCharges = StayCharges() + MisCharges()
		lblTotal.Text = FormatCurrency(DecTotalCharges)

	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

		'Clear form

		tbxdays.Clear()
		tbxMeds.Clear()
		tbxSurgical.Clear()
		tbxLab.Clear()
		tbxRehab.Clear()
		lblTotal.Text = String.Empty


	End Sub

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

		'Close form

		Me.Close()

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1