Results 1 to 9 of 9

Thread: Computing Average FIGURED OUT

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    Computing Average FIGURED OUT

    I have two text boxes that are blank until I click a btnCalculate.
    I need to get an average of those two text boxes. The text boxes are named SP = txtSalepersons.Text and TS = txtTotalSales.Text
    I need to get the TS / SP. But when I write:

    txtAverageSales.Text = TS / SP

    it doesn't work because the first time there are no values to average. After the first time it works like a charm b/c values are in the box.

    So basically the first time I click it I just want it to display TS and then after the first time display TS / SP. Please help, I think I might need an if then statement.
    Last edited by twisted; Jun 8th, 2004 at 12:33 PM.
    Twisted

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    If your posted code works, then you are using VB6.

    This is the VB.NET section.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    VB Code:
    1. 'Put this inside the button code
    2.  
    3. If txtSalesPersons.Text.Trim <> ""
    4.  
    5. 'Check that both boxes are numeric
    6.       If IsNumeric(txtAverageSales.Text) = True And IsNumeric(txtSalepersons.Text) = True Then
    7.  
    8.            'Check that the Salespersons box is not 0 as you cannot divide by 0
    9.  
    10.             If Decimal.Parse(txtSalepersons.Text) = 0 Then
    11.                   Messagebox.Show("Cannot divide a number by 0, salespersons box cannot be 0.")
    12.             Else
    13.                    txtAverageSales.Text = CStr(Decimal.Parse(txtTotalSales.Text) / Decimal.Parse(txtSalepersons.Text))
    14.             End If
    15.       Else
    16.            Messagebox.Show("Both values must be numeric.")
    17.       End If
    18. Else
    19.  
    20.      If IsNumeric(txtAverageSales.Text) = True Then
    21.  
    22.            txtAverageSales.Text = txtTotalSales.Text
    23.      Else
    24.           Messagebox.Show("Enter a numeric value for average sales")
    25.      End If
    26. End If

    I am assuming by your code that there is something in the TotalSales text box the first time you click as in your description you said the first time you click it, it will display the Total Sales.

    The important part to you is the very first line which checks if there is anything in the salespersons textbox. The other stuff is largely included to validate the information in the text boxes.

    You will probably also want to format the number when it computes the average as it could potentially display alot of significant figures. e.g. 1 /3 could be 0.33333...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    This is good, but totally not what I needed. Here is a link to show you what I am building..

    WHAT I AM BUILDING

    Now my problem is Average Sales. I just thought it was Total Sales divided by Salespersons. Which it is, but the first time I click Gross Pay it shows NaN in my Average Sales box. I think this is because it is trying to divide values that are not there yet. But the second time I hit Gross Pay it works fine because values are already there. How do I get it to return the value you enter in Sales Amount (since the first time Total Sales is the Sales Amount and Totals Sales / 1 salesperson is the same number.
    Twisted

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    Us a simple If..Then clause


    VB Code:
    1. If SP=0 then
    2.   SP=1
    3.   txtAverageSales.Text = TS / SP
    4.   SP=0
    5. else
    6.   txtAverageSales.Text = TS / SP
    7. End If
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by taxes
    HI,

    Us a simple If..Then clause


    VB Code:
    1. If SP=0 then
    2.   SP=1
    3.   txtAverageSales.Text = TS / SP
    4.   SP=0
    5. else
    6.   txtAverageSales.Text = TS / SP
    7. End If
    That didn't work for some reason, but Yes that is basically what I am trying to do. Here is the code I have for the btnClick:
    VB Code:
    1. ' Set up constants for Base pay(BP) = $500 and commission(CM) is 6%
    2.     Const BP As Double = 500.0
    3.     Const CM As Double = 0.06
    4.     ' Set up variables for sales, gross pay, salespersons, total sales,
    5.     ' and average sales.
    6.     Dim SA, GP, SP, TS, AV As Double
    7.  
    8.  
    9.     Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
    10.         ' name abbreviations
    11.         SA = Val(nudSales.Text)
    12.         SP = Val(grpSalespersons.Text)
    13.         TS = Val(grpTotalSales.Text)
    14.         ' Compute value and display in Gross Pay
    15.         GP = BP + SA * CM
    16.         txtGross.Text = Format(GP, "$#####0.00")
    17.         ' Compute value and display in Salespersons
    18.         grpSalespersons.Text = (SP + 1).ToString
    19.         ' Compute value and display in Total Sales
    20.         grpTotalSales.Text = (SA + TS).ToString
    21.         ' Compute value and display in Average Sales
    22.        
    23.     End Sub
    I don't have anything for Average Sales b/c I can't figure it out!
    Twisted

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by taxes
    HI,

    Us a simple If..Then clause


    VB Code:
    1. If SP=0 then
    2.   SP=1
    3.   txtAverageSales.Text = TS / SP
    4.   SP=0
    5. else
    6.   txtAverageSales.Text = TS / SP
    7. End If
    This is what I had to do to get it to work right, but thanks for getting me on the right track.
    VB Code:
    1. If SP = 0 Then
    2.             grpAverageSales.Text = (SA).ToString
    3.         Else
    4.             grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
    5.  
    6.         End If
    Twisted

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Yep. My mistake, I copied it from my VB6 file instead of my VB.NET.

    BUT looking at your other threads, if you included code to increase your grpSalespersons.Text ( and SP) by 1 every time you confirmed the extra sale and before you computed the average sales, you would do away with this step altogether.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Thanks once again my VB savior!
    Twisted

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