|
-
Jun 8th, 2004, 07:05 AM
#1
Thread Starter
Addicted Member
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
-
Jun 8th, 2004, 08:28 AM
#2
PowerPoster
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.
-
Jun 8th, 2004, 08:41 AM
#3
Addicted Member
VB Code:
'Put this inside the button code
If txtSalesPersons.Text.Trim <> ""
'Check that both boxes are numeric
If IsNumeric(txtAverageSales.Text) = True And IsNumeric(txtSalepersons.Text) = True Then
'Check that the Salespersons box is not 0 as you cannot divide by 0
If Decimal.Parse(txtSalepersons.Text) = 0 Then
Messagebox.Show("Cannot divide a number by 0, salespersons box cannot be 0.")
Else
txtAverageSales.Text = CStr(Decimal.Parse(txtTotalSales.Text) / Decimal.Parse(txtSalepersons.Text))
End If
Else
Messagebox.Show("Both values must be numeric.")
End If
Else
If IsNumeric(txtAverageSales.Text) = True Then
txtAverageSales.Text = txtTotalSales.Text
Else
Messagebox.Show("Enter a numeric value for average sales")
End If
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...
-
Jun 8th, 2004, 09:12 AM
#4
Thread Starter
Addicted Member
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.
-
Jun 8th, 2004, 10:14 AM
#5
PowerPoster
HI,
Us a simple If..Then clause
VB Code:
If SP=0 then
SP=1
txtAverageSales.Text = TS / SP
SP=0
else
txtAverageSales.Text = TS / SP
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.
-
Jun 8th, 2004, 11:30 AM
#6
Thread Starter
Addicted Member
Originally posted by taxes
HI,
Us a simple If..Then clause
VB Code:
If SP=0 then
SP=1
txtAverageSales.Text = TS / SP
SP=0
else
txtAverageSales.Text = TS / SP
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:
' Set up constants for Base pay(BP) = $500 and commission(CM) is 6%
Const BP As Double = 500.0
Const CM As Double = 0.06
' Set up variables for sales, gross pay, salespersons, total sales,
' and average sales.
Dim SA, GP, SP, TS, AV As Double
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SA = Val(nudSales.Text)
SP = Val(grpSalespersons.Text)
TS = Val(grpTotalSales.Text)
' Compute value and display in Gross Pay
GP = BP + SA * CM
txtGross.Text = Format(GP, "$#####0.00")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP + 1).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (SA + TS).ToString
' Compute value and display in Average Sales
End Sub
I don't have anything for Average Sales b/c I can't figure it out!
-
Jun 8th, 2004, 12:33 PM
#7
Thread Starter
Addicted Member
Originally posted by taxes
HI,
Us a simple If..Then clause
VB Code:
If SP=0 then
SP=1
txtAverageSales.Text = TS / SP
SP=0
else
txtAverageSales.Text = TS / SP
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:
If SP = 0 Then
grpAverageSales.Text = (SA).ToString
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
End If
-
Jun 8th, 2004, 12:42 PM
#8
PowerPoster
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.
-
Jun 8th, 2004, 12:45 PM
#9
Thread Starter
Addicted Member
Thanks once again my VB savior!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|