Results 1 to 9 of 9

Thread: Order Status problem VB Studio 2013

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    21

    Order Status problem VB Studio 2013

    I have to create a form where you enter the number of spools ordered and then it brings an input box up to ask you the amount of spools in stock.
    It then tells you the amount of spools ready to ship, the amount of spools on back order, the shipping and handling charges, and the total due

    My problem is when I enter 500 spools ordered and 200 spools in stock, it returns 0 spools ready to ship, 500 spools on back order, and the shipping and total due is $0.00

    Why is this happening? Am I not calling my functions correctly? it should display
    Ready to Ship: 200
    Back Order: 300
    Shipping and handling: $2000.00
    Total Due: 22,000.00


    Name:  Capture.JPG
Views: 1046
Size:  30.1 KB
    Code:
    Public Class Form1
        Private intSpoolsOrdered As Integer 'to hold the amount of spools ordered
        Private intStock As Integer
        Private intReadyToShip As Integer
        Private intShippingCharges As Integer
    
        Private Function ValidateInputFields() As Boolean
            If Not Integer.TryParse(txtSpoolsOrdered.Text, intSpoolsOrdered) Or intSpoolsOrdered < 0 Then
                MessageBox.Show("Please enter a positive integer of 1 or more for Spools Ordered")
                Return False
            End If
            Return True
        End Function
    
        Public Function GetInStock(ByVal intStock As Integer) As Integer
            Try
                'get the stock from the user
                intStock = CInt(InputBox("Please enter the amount of spools in stock."))
                If intStock < 0 Then
                    MessageBox.Show("Please enter a positive integer only.")
                End If
            Catch ex As Exception
                MessageBox.Show("Please enter a positive integer.")
            End Try
            Return intStock
        End Function
    
        Public Function ReadyToShip(ByVal intStock As Integer, ByVal intSpoolsOrdered As Integer) As Integer
            Dim intReadyToShip As Integer
    
            If intSpoolsOrdered >= intStock Then
                intReadyToShip = intStock
            ElseIf intSpoolsOrdered < intStock Then
                intReadyToShip = intSpoolsOrdered
            End If
            Return intReadyToShip
        End Function
    
        Public Function BackOrdered(ByVal intStock As Integer, ByVal intSpoolsOrdered As Integer) As Integer
            Dim intBackOrdered As Integer
    
            If intStock >= intSpoolsOrdered Then
                intBackOrdered = 0
            ElseIf intStock < intSpoolsOrdered Then
                intBackOrdered = intSpoolsOrdered - intStock
            End If
            Return intBackOrdered
        End Function
    
        Public Function ShippingCharges(ByVal intReadyToShip As Integer, ByVal intShippingCharges As Integer) As Integer
            Dim intShippingPrice As Integer 'price of shipping and handling
    
            'set the shipping charges
            If chkRushDelivery.Checked = True Then
                intShippingCharges = 15
            Else
                intShippingCharges = 10
            End If
    
            intShippingPrice = intShippingCharges * intReadyToShip
            Return intShippingPrice
        End Function
    
    
    
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim dblTotalDue As Double
    
    
    
            If ValidateInputFields() = True Then
                'get the current stock of spools
                GetInStock(intStock)
    
                'display the amount of spools ready to ship
                lblReadyToShip.Text = ReadyToShip(intStock, intSpoolsOrdered).ToString
    
                'display the amount of spools on back order
                lblBackOrder.Text = BackOrdered(intStock, intSpoolsOrdered).ToString
    
                'display the shipping and handling charges
                lblShipping.Text = ShippingCharges(intReadyToShip, intShippingCharges).ToString("c")
    
                'display the total due
                dblTotalDue = (ReadyToShip(intStock, intSpoolsOrdered) * 100) + ShippingCharges(intReadyToShip, intShippingCharges)
                lblTotal.Text = dblTotalDue.ToString("c")
    
            End If
    
        End Sub
    End Class

  2. #2
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Order Status problem VB Studio 2013

    You're putting the functions into labels, which is totally wrong.
    We put values, not functions.

    Code:
     txtReadyToShip.Text = ReadyToShip(intStock, intSpoolsOrdered).ToString
    
                'display the amount of spools on back order
                txtBackOrder.Text = BackOrdered(intStock, intSpoolsOrdered).ToString
    
                'display the shipping and handling charges
                txtShipping.Text = ShippingCharges(intReadyToShip, intShippingCharges).ToString("c")
    
                'display the total due
                dblTotalDue = (ReadyToShip(intStock, intSpoolsOrdered) * 100) + ShippingCharges(intReadyToShip, intShippingCharges)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    21

    Re: Order Status problem VB Studio 2013

    I am not quite sure what to do if you are saying that i cannot do the below:
    i cant make the text of a label equal the calculations of my function and turn it into a string?
    I have changed it and I still get the same result so I don't think that was the problem.
    Code:
     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim intReadyToShip As Integer
            Dim intBackOrdered As Integer
            Dim intShippingCharges As Integer
            Dim dblTotalDue As Double
    
            If ValidateInputFields() = True Then
                'get the current stock of spools
                GetInStock(intStock)
    
                'display the amount of spools ready to ship
                intReadyToShip = ReadyToShip(intStock, intSpoolsOrdered)
                lblReadyToShip.Text = intReadyToShip.ToString
    
                'display the amount of spools on back order
                intBackOrdered = BackOrdered(intStock, intSpoolsOrdered)
                lblBackOrder.Text = intBackOrdered.ToString
    
                'display the shipping and handling charges
                intShippingCharges = ShippingCharges(intReadyToShip, intShippingCharges)
                lblShipping.Text = intShippingCharges.ToString("c")
    
                'display the total due
                dblTotalDue = (ReadyToShip(intStock, intSpoolsOrdered) * 100) + ShippingCharges(intReadyToShip, intShippingCharges)
                lblTotal.Text = dblTotalDue.ToString("c")
    
            End If

  4. #4
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Order Status problem VB Studio 2013

    BackOrdered,ShippingCharges,ReadyToShip are functions,I repeat.
    We put values returned by such functions in controls such labels,...
    For instance values returned could be intStock,intReadyToShip,intBackOrdered.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Order Status problem VB Studio 2013

    putting the funtion return values into a label isn't a problem... but there is an issue with how your funtions are setup and being called...


    Let's start with the first one....
    Code:
        Public Function GetInStock(ByVal intStock As Integer) As Integer
            Try
                'get the stock from the user
                intStock = CInt(InputBox("Please enter the amount of spools in stock."))
                If intStock < 0 Then
                    MessageBox.Show("Please enter a positive integer only.")
                End If
            Catch ex As Exception
                MessageBox.Show("Please enter a positive integer.")
            End Try
            Return intStock
        End Function
    First, it returns a value, so so far so good... but then you also pass in a parameter... there isn't a reason for that. Especially when you're returning it. And to top it off, intStock is a form level varaible... again... not needed.
    Code:
        Public Function GetInStock() As Integer 
           Dim InStockCount as Integer = 0
            Try
                'get the stock from the user
                InStockCount = CInt(InputBox("Please enter the amount of spools in stock."))
                If InStockCount < 0 Then
                    MessageBox.Show("Please enter a positive integer only.")
                End If
            Catch ex As Exception
                MessageBox.Show("Please enter a positive integer.")
            End Try
            Return InStockCount
        End Function
    Then you call it like this:
    Code:
    intStock = GetSTock()
    You've got similar issues with your other functions. Something to note, you're using ByVal on parameters, changing them and expecting them to keep their changes... when you use ByVal you're sending a copy of the VALUE... where as if you use ByRef, you get a REFERENCE to the item.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Order Status problem VB Studio 2013

    Quote Originally Posted by techgnome View Post
    putting the funtion return values into a label isn't a problem...

    -tg
    By trying to help, I learnt something new, thanks.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    21

    Re: Order Status problem VB Studio 2013

    Quote Originally Posted by techgnome View Post
    putting the funtion return values into a label isn't a problem... but there is an issue with how your funtions are setup and being called...


    Let's start with the first one....
    Code:
        Public Function GetInStock(ByVal intStock As Integer) As Integer
            Try
                'get the stock from the user
                intStock = CInt(InputBox("Please enter the amount of spools in stock."))
                If intStock < 0 Then
                    MessageBox.Show("Please enter a positive integer only.")
                End If
            Catch ex As Exception
                MessageBox.Show("Please enter a positive integer.")
            End Try
            Return intStock
        End Function
    First, it returns a value, so so far so good... but then you also pass in a parameter... there isn't a reason for that. Especially when you're returning it. And to top it off, intStock is a form level varaible... again... not needed.
    Code:
        Public Function GetInStock() As Integer 
           Dim InStockCount as Integer = 0
            Try
                'get the stock from the user
                InStockCount = CInt(InputBox("Please enter the amount of spools in stock."))
                If InStockCount < 0 Then
                    MessageBox.Show("Please enter a positive integer only.")
                End If
            Catch ex As Exception
                MessageBox.Show("Please enter a positive integer.")
            End Try
            Return InStockCount
        End Function
    Then you call it like this:
    Code:
    intStock = GetSTock()
    You've got similar issues with your other functions. Something to note, you're using ByVal on parameters, changing them and expecting them to keep their changes... when you use ByVal you're sending a copy of the VALUE... where as if you use ByRef, you get a REFERENCE to the item.

    -tg
    wow thanks a lot man!!! I put the ByRef where I was changing the value and Wallah!!! It works!!!

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Order Status problem VB Studio 2013

    But did you make all of the other changes too? Or understand what changes I made and why?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    21

    Re: Order Status problem VB Studio 2013

    I kind of understand what you are saying, basically I am declaring the variable too many times when I dont have to
    i dont fully understand the concept of when to not pass the parameters because in the book they do it every time so that is why I did it every time in each function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width