Need help with VB.net involving Totals

Okay guys, here I am again. I need help on adding totals together.

What I want is to have a bsic commission calculator that totals different things and will display them later on.

Here's what I have so far, some works, some doesn't.

Public Class VBACComCalc

Inherits System.Windows.Forms.Form

[Windows Form Designer generated code]

Const Quota As Integer = 1000D

Const CommissionRate As Integer = 0.15D

Const BasePay As Integer = 250D

Dim SalespersonName As String = txtSalespersonName.Text (When I run the program, this line comes up as an error and says "Object reference not set to an instance of an object." But the name is right.)

Dim WeeklySales As Integer = txtWeeklySales.Text (same here)

Dim SalesTotal As Integer

Dim CommissionTotal As Integer

Dim PayTotal As Integer

Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click

MessageBox.Show("Programmer: Paul Richardson" & ControlChars.NewLine & "March 2006", "VB Auto Center ~Commission Calculator~", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click

Me.Close()

End Sub

Private Sub mnuEditFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditFont.Click

With dlgFont

.ShowDialog()

lblEarned.Font = .Font

End With

End Sub

Private Sub mnuEditColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditColor.Click

With dlgColor

.ShowDialog()

lblEarned.ForeColor = .Color

End With

End Sub

Private Sub mnuEditClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditClear.Click

With txtSalespersonName()

.Clear()

.Focus()

End With

txtWeeklySales.Clear()

lblEarned.ResetText()

End Sub

Private Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click

ppdPrintPreview.Document = prtPrintDocument

ppdPrintPreview.ShowDialog()

End Sub

Private Sub prtPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prtPrintDocument.PrintPage

Dim fntPrintFont As New Font("Arial", 12)

Dim sngLineHeight As Single = fntPrintFont.GetHeight + 2

Dim sngX As Single = e.MarginBounds.Left

Dim sngY As Single = e.MarginBounds.Top

e.Graphics.DrawString("Programmer: Paul Richardson", fntPrintFont, Brushes.Black, sngX, sngY)

sngY += sngLineHeight * 2

e.Graphics.DrawString("Valley Boulevard Auto Center Sales Information", fntPrintFont, Brushes.Black, sngX, sngY)

sngY += sngLineHeight * 2

e.Graphics.DrawString("Sales Total: " & FormatCurrency(SalesTotal), fntPrintFont, Brushes.Black, sngX, sngY)

sngY += sngLineHeight * 2

e.Graphics.DrawString("Commission Total: " & FormatCurrency(CommissionTotal), fntPrintFont, Brushes.Black, sngX, sngY)

sngY += sngLineHeight * 2

e.Graphics.DrawString("Total Pay For All Salespersons: " & FormatCurrency(PayTotal), fntPrintFont, Brushes.Black, sngX, sngY)

sngY += sngLineHeight * 2

End Sub

Private Sub mnuFileSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSummary.Click

Dim strMessage As String = "Sales Total: " & FormatCurrency(SalesTotal) & ControlChars.NewLine & "Commission Total: " & FormatCurrency(CommissionTotal) & ControlChars.NewLine & "Total Pay For All Salespersons: " & FormatCurrency(PayTotal)

MessageBox.Show(strMessage, "VB Auto Center Sales Summary", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Function CalculateSales()

Dim WeeklyPay As Integer

Dim SalespersonPay As Integer

If WeeklySales >= Quota Then

WeeklyPay = WeeklySales * CommissionRate

SalesTotal += WeeklyPay

Else

WeeklyPay = BasePay

SalesTotal += WeeklyPay

End If

End Function

^This is all wrong I think^

Private Sub mnuFilePay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePay.Click

' Here should be the code to display (Name) earned (WeeklySales + Commission (if any)) in lblEarned And probably some other stuff...Accumulators

End Sub

End Class

Thanks to whoever can help me with this.




Answer this question

Need help with VB.net involving Totals

  • Edward Stephen

    Well can I just send you the program The thing that's wrong is the entire Function. Everything else works.

  • JHW

    Sure - send it to christian dot graus at gmail dot com.

  • GP.Software

    I sent it, thanks.

  • Mikey Stevey

    What's the overall flow Where does it break down You said that you're keeping a total and adding to it when a menu item is selected ( have you thought about a button BTW, it seems like a pain to go to a menu for every sale ), and then you display it at the end, right So, the values you end up with are wrong Have you tried putting in messageboxes to show the cumulative totals to see where they become wrong Perhaps they need to be reset at some point, and carry over an old value

    I'm really trying to be helpful, but I feel like I'm working in the dark here...



  • Sherif Mahmoud

    I'm not sure I follow here - does the commented out stuff do the incrementing to get the totals If so, what is the problem Are the values being added always zero Or what is going wrong

    Have you tried setting a breakpoint ( F9 ) and stepping through this code to see where it's not doing what you expect



  • Maulik Soni

    I'm afraid if you'd taken my advice you probably would have found your core error pretty quickly. All calculations came to 0 because of this line

    Const CommissionRate As Int= 0.15D

    C# would have given you a warning, VB silently stored 0 as commission rate, having rounded .15 to an int. If you convert the type of this variable from int to double, and then move the commented out code around so that inside a new menu event handler for the menu item like this :

    Private Sub mnuFilePay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePay.Click

    Dim Name As String = txtName.Text

    Dim WeeklySales As Integer = txtWeeklySales.Text

    Dim WeeklyPay As Integer = 0

    Dim SalespersonPay As Integer

    If WeeklySales >= Quota Then

    WeeklyPay = WeeklySales * CommissionRate

    SalesTotal += WeeklyPay

    CommissionTotal += WeeklyPay

    Else

    WeeklyPay = BasePay

    SalesTotal += WeeklyPay

    End If

    PayTotal += WeeklyPay

    lblEarned.Text = (Name) & " earned a total pay of " & (WeeklyPay)

    End Sub

    I would reiterate, this UI is a pain. You shoujld have buttons so that these things can be done with one click instead of two. Also, do your reps really get paid more for selling 999 items than they do for selling 1000



  • niknaksbarbeque

    To be honest, I'm still trying to understand the problem. Did you want me to just write all the code for you I know if I ever have to ask for help, I would prefer to be guided to a solution so I understand how to do the task in question the next time, but at this point, I still don't see where your problem lies.

    You have an event handler for the menu item. This code should add a value to your two totals. I've not seen the code that does this yet, but in theory, the code you're posting looks fine, assuming the variables being used are correct to start with.

    Have you used the debugger before If not, you really need to learn how to. You'll probably solve your own problem as soon as you know the line of code you need to look at closely to work out what's going wrong.



  • tecxx

    Dim SalespersonName As String = txtSalespersonName.Text (When I run the program, this line comes up as an error and says "Object reference not set to an instance of an object." But the name is right.)

    This code will run before the textbox is created. You should just access the text property when you need it.

    ^This is all wrong I think^

    What's it doing and what do you want it to do

     Here should be the code to display (Name) earned (WeeklySales + Commission (if any)) in lblEarned   And probably some other

    lblEarned.Text = txtSalespersonName.Text  & " " & WeeklySales + Commission & " " & otherstuff :-)

     

     



  • Julie Knibbe

    Alright, I got it working, thanks man. I'll post when it's complete.



  • deanhully

    I just don't know what code to write, mind helping me out

  • Roger Haight MSFT

    Yeah, I know how to use breakpoints and debug projects, but my code is right. I just don't know what to write to have the Totals add up (3 by the way; Sales, Commission, and Pay.)

  • Sammy1971

    Well, It's just not working.

    I want it to have Something with an If...Then statement that is like:

    If WeeklySales >= Quota Then

    Commission = WeeklySales * Commission Rate

    WeeklyPay = BasePay + Commission

    (Increment stuff here)

    Else

    WeeklyPay = BasePay

    (Increment stuff here, but not commission)

    End If

    The stuff that needs to be displayed in the Summary msgBox is

    "Sales Total: " & FormatCurrency(SalesTotal) & ControlChars.NewLine & "Commission Total: " & FormatCurrency(CommissionTotal) & ControlChars.NewLine & "Total Pay For All Salespersons: " & FormatCurrency(PayTotal)

    So I need the sale that just occured when mnuFilePay is clicked to add into the totals.

    WeeklySales goes into SalesTotal

    Commissions go into CommissionTotal

    And the amount that is displayed in lblEarned goes into PayTotal

    I'm not sure on how to make them do that...

    Thanks in advance. Let me know if you want the entire file, I'll mail you it.



  • Need help with VB.net involving Totals