1 Replies - 601 Views - Last Post: 06 October 2012 - 10:13 PM Rate Topic: -----

#1 NewToVBinAZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 12

Where to place data validation in a For-Next loop

Posted 06 October 2012 - 08:24 PM

This is my first class in VB. Consider me a VB kinder-gardener. The assignment is to prompt for number of rooms occupied for each of 8 floors in a hotel using an input box, compute floor occupancy, display in a list box the number of rooms occupied and occupancy percentage by floor (occ % = rooms occupied/rooms available), accumulate total rooms occupied and also display total rooms occupied for whole hotel, and whole hotel occupancy.

My code works but if data entered is more than the max number per floor (30) I can get a message box to show the data error, but I don't know how to stop calculating and displaying occupancy for the floor until I get a good number input.

I'm sure it's simple and if anyone can point me in the right direction I would appreciate it.


    Private Sub btnReport_Click(sender As System.Object, e As System.EventArgs) Handles btnReport.Click
        Const dblTOTAL_ROOMS As Double = 240        ' total number of rooms in hotel
        Const dblFLOOR_ROOMS As Double = 30         ' total number of rooms per floor   
        Dim intFloorCount As Integer                ' loop counter
        Dim dblRooms, dblTotalRooms As Double       ' to hold rooms per floor input and accumulator for total rooms
        Dim strInput As String                       ' to hold user input 
        Dim decFloorOccupancy, decTotalOccupancy As Decimal ' to hold floor occupancy and total occupancy percentages

        For intFloorCount = 1 To 8
            'prompt user for number of rooms occupied for the floor
            strInput = InputBox("Enter # Rooms Occupied on Floor # " & intFloorCount.ToString())

            'convert the input to to a double
            Double.TryParse(strInput, dblRooms)

            'compute the floor occupancy
            decFloorOccupancy = dblRooms / dblFLOOR_ROOMS

            'create a string to display floor occupancy
            lstFloors.Items.Add("Floor " & intFloorCount.ToString() & " Rooms Occupied: " &
                dblRooms.ToString() & "  " & "Occupancy Rate is " & decFloorOccupancy.ToString("p"))

            'add the rooms to the accumulator
            dblTotalRooms += dblRooms

        Next

        'calculate the total occupancy
        decTotalOccupancy = dblTotalRooms / dblTOTAL_ROOMS

        'display total rooms ococupied and total occupancy

        lblRooms.Text = dblTotalRooms.ToString()

        lblTotalOccupancy.Text = decTotalOccupancy.ToString("p")



    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Where to place data validation in a For-Next loop

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Where to place data validation in a For-Next loop

Posted 06 October 2012 - 10:13 PM

You could convert the strinput, after it's received a value, to a double and check if the value is less than or equal to 30. You'll also have to check to make sure the value is numeric. You could place it just after strinput is given a value. You can use a select case statement, an if statement, or whatever way you come up with I doubt any can be more taxing than the rest in any other aspect than readability.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1