Solved

help with code

Posted on 2008-10-05
14
207 Views
Last Modified: 2010-04-30
i have this code it is supposed to calculate tax which is allSubtotals * 0.07 when i run the program everything work but the Tax and the Total. can you tell me what im missing in my code. thank you.
this for the Tax
Private Sub Tax_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
 
        TextBox11.Text = "$0.00"
        Const TaxRAte As Single = 0.07
        Dim AllSubtotals As Double
        Tax = AllSubtotals * TaxRAte
        TextBox10.Text = CStr(Tax)
        TextBox10.Text = Format(Tax, "$#,##0.00")
    End Sub
 
this for the total. 
 
Private Sub Total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged, TextBox10.TextChanged, TextBox11.TextChanged
        TextBox12.Text = "$0.00"
        'Declaring Variables
        Dim Total As Double
        If TextBox8.Text.Length < 1 Then Exit Sub
        If TextBox9.Text.Length < 1 Then Exit Sub
        If TextBox10.Text.Length < 1 Then Exit Sub
        Total = Tax + AllSubtotals - Discount
        TextBox12.Text = CStr(Total)
        TextBox12.Text = Format(Total, "$#,##.00")
    End Sub

Open in new window

0
Comment
Question by:elrayiss
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22645744
>>Dim AllSubtotals As Double
>>Tax = AllSubtotals * TaxRAte

There is no value in the AllSubTotals variable. It should be set with a value.

>>Total = Tax + AllSubtotals - Discount

Where is AllSubTotals declared? In the previous event? It is not visible outside the event. Add "option explicit on" at the top of your class and it will discover that kind of error.
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 22645746
Look at these two lines.

        Dim AllSubtotals As Double
        Tax = AllSubtotals * TaxRAte

Add these two lines immediately after the two lines above.

msgbox("Here is tax! [" & tax &']")
msgbox("Here is AllSubtotals " & AllSubtotals)
0
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 22647064
I am not sure if you are using strict mode or not but I am seeing a lot of problems with variable scope.  I am assuming right now that the you have the varialbe Tax, AllSubtotals, and Discout set as public/global variables.

1.  In your Tax_TextChanged event, you are not assigning any value to AllSubtotals  before using it.  Do you want to use your global/public variables instead of declaring it again?
2.  In your Total_TextChanged event, you are missing End If statements for lines 18,19, and 20
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:Jeff Darling
ID: 22649998
The problem is exactly as emoreau and leikelman describe. I suggested putting the msgboxes in there so you could see the problem.  

I have another suggestion that may be helpful. Change the names of the textboxes to be descriptive.

As I  look at the code, I'm finding it difficult to tell what Textbox8, Textbox9, Textbox10, Textbox11 and Textbox12 actually contain.  Can you describe them?

For example, if Textbox10 is the calculated tax, how about naming the textbox txtCalcTax.
0
 

Author Comment

by:elrayiss
ID: 22650409
ok i will post all my program with the textboxes names hope that you can help then.
textbox1 numberofnights
textbos2 costpernight
textbox3 subtotal
textbox4 roomservice
textbox5 telephoen
textbox6 laundry
textbox7 Mysubtotal
checkbox1
8 code
recalculate button1
9 allsubtotals
10 discount
11 tax
12 total

Option Explicit On
Option Strict On
Public Class Form1
 
 
    Private Total As Double
    Private Subtotal As Double
    Private AllSubtotals As Double
    Private Tax As Double
    Private Discount As Double
 
 
    Private Sub NumberOfNights_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
 
    End Sub
 
    Private Sub CostPerNight_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
 
    End Sub
 
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        'Value for TextbBox3
        TextBox3.Text = "$0.00"
 
        'Declaring variables
        Dim dblCostPerNight As Double
        Dim dblNumberOfNights As Double
 
 
        If TextBox1.Text.Length = 0 AndAlso TextBox2.Text.Length = 0 Then Exit Sub
        If Not IsNumeric(TextBox2.Text) OrElse Not IsNumeric(TextBox1.Text) Then Exit Sub
 
        dblNumberOfNights = CDbl(TextBox1.Text)
        dblCostPerNight = CDbl(TextBox2.Text)
        Total = dblNumberOfNights * dblCostPerNight
        TextBox3.Text = CStr(Total)
        TextBox3.Text = Format(Total, "$#,##0.00")
    End Sub
 
    Private Sub RoomService_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
 
    End Sub
 
    Private Sub Telephone_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
 
    End Sub
 
    Private Sub Laundry_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
 
    End Sub
 
    Private Sub MySubtotal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged
        'Value for Subtotal
        TextBox7.Text = "$0.00"
        Dim dblRoomService As Double
        Dim dblTelephone As Double
        Dim dblLaundry As Double
 
 
 
        If TextBox4.Text.Length = 0 AndAlso TextBox5.Text.Length = 0 AndAlso TextBox6.Text.Length = 0 Then Exit Sub
        If Not IsNumeric(TextBox4.Text) OrElse Not IsNumeric(TextBox5.Text) OrElse Not IsNumeric(TextBox6.Text) Then Exit Sub
 
        dblRoomService = CDbl(TextBox5.Text)
        dblTelephone = CDbl(TextBox4.Text)
        dblLaundry = CDbl(TextBox6.Text)
        Subtotal = dblRoomService + dblTelephone + dblLaundry
        TextBox7.Text = CStr(Subtotal)
        TextBox7.Text = Format(Subtotal, "$#,##0.00")
    End Sub
 
 
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        'Value of TextBox8
        TextBox8.Text = "$0.00"
        TextBox8.Enabled = CheckBox1.Checked 'allow the user to input when the ckeckbox is checked
        Button1.Enabled = CheckBox1.Checked 'allow the user to recaculate the total after the discount given
 
    End Sub
 
    Private Sub AllSubtotals_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged, TextBox7.TextChanged
        'Value for AllSubtotals
        TextBox9.Text = "$0.00"
        'Declaring Variable
 
 
        'Function to find AllSubtotals
        AllSubtotals = Total + Subtotal
        TextBox9.Text = CStr(AllSubtotals)
        TextBox9.Text = Format(AllSubtotals, "$#,##0.00")
    End Sub
 
    Private Sub Code_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged, TextBox10.TextChanged
        If TextBox8.Text = "SAVE50D" Then
            Discount = AllSubtotals - 50
        Else
            Discount = 0
        End If
        If TextBox8.Text = "SAVE20P" Then
            Discount = 0.2 * AllSubtotals
        End If
        TextBox10.Text = Format(Discount, "$#,##0.00")
    End Sub
 
    Private Sub Tax_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
 
        TextBox11.Text = "$0.00"
        Const TaxRAte As Single = 0.07
        Dim AllSubtotals As Double
        Tax = AllSubtotals * TaxRAte
        TextBox10.Text = CStr(Tax)
        TextBox10.Text = Format(Tax, "$#,##0.00")
    End Sub
 
    Private Sub Total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged, TextBox10.TextChanged, TextBox11.TextChanged
        TextBox12.Text = "$0.00"
        'Declaring Variables
        Dim Total As Double
        If TextBox8.Text.Length < 1 Then Exit Sub
        If TextBox9.Text.Length < 1 Then Exit Sub
        If TextBox10.Text.Length < 1 Then Exit Sub
 
        Total = Tax + AllSubtotals - Discount
        TextBox12.Text = CStr(Total)
        TextBox12.Text = Format(Total, "$#,##.00")
    End Sub
 
    Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Ctl As Control
        For Each Ctl In Me.Controls
            If TypeOf Ctl Is TextBox Then
                Ctl.Text = ""
            End If
        Next
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const Code As String = "the term must be SAVE20P OR SAVE50D)"
 
 
        Double.TryParse(Me.TextBox8.Text, CDbl(Code))
        Double.TryParse(Me.TextBox9.Text, AllSubtotals)
        Double.TryParse(Me.TextBox9.Text, Discount)
        Double.TryParse(Me.TextBox9.Text, Total)
        'REDO THE CALCULATION'
        If TextBox8.Text = "SAVE50D" Then
            Total = AllSubtotals - 50 + Tax
        Else
            Total = AllSubtotals + Tax
        End If
        If TextBox8.Text = "SAVE20P" Then
            Total = 0.2 * AllSubtotals + Tax
        Else : Total = AllSubtotals + Tax
 
        End If
        TextBox12.Text = Format(Discount, "$#,##0.00")
    End Sub
End Class

Open in new window

0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 22651576
there were multiple errors like:
-variables declared in methods shadowing class level variables with the same name
-invalid textbox affected
-re-entring event

The final code should read like this:
Option Strict On
 
Public Class Form1
 
    Private mblnResetting As Boolean
 
    Private Total As Double
    Private GrandTotal As Double
    Private Subtotal As Double
    Private AllSubtotals As Double
    Private Tax As Double
    Private Discount As Double
 
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckBox1.Checked = True
    End Sub
 
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        'Value for TextbBox3
        TextBox3.Text = "$0.00"
 
        If mblnResetting Then
            Total = 0
        Else
            'Declaring variables
            Dim dblCostPerNight As Double
            Dim dblNumberOfNights As Double
 
            Double.TryParse(TextBox1.Text, dblNumberOfNights)
            Double.TryParse(TextBox2.Text, dblCostPerNight)
 
            Total = dblNumberOfNights * dblCostPerNight
        End If
        TextBox3.Text = Format(Total, "$#,##0.00")
    End Sub
 
    Private Sub MySubtotal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged
        'Value for Subtotal
        TextBox7.Text = "$0.00"
 
        If mblnResetting Then
            Subtotal = 0
        Else
            Dim dblRoomService As Double
            Dim dblTelephone As Double
            Dim dblLaundry As Double
 
            Double.TryParse(TextBox4.Text, dblTelephone)
            Double.TryParse(TextBox5.Text, dblRoomService)
            Double.TryParse(TextBox6.Text, dblLaundry)
 
            Subtotal = dblRoomService + dblTelephone + dblLaundry
        End If
        TextBox7.Text = Format(Subtotal, "$#,##0.00")
    End Sub
 
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        TextBox8.Enabled = CheckBox1.Checked 'allow the user to input when the ckeckbox is checked
        Button1.Enabled = CheckBox1.Checked 'allow the user to recaculate the total after the discount given
        If Not TextBox8.Enabled Then
            TextBox8.Text = "$0.00"
        End If
    End Sub
 
    Private Sub AllSubtotals_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged, TextBox7.TextChanged
        'Value for AllSubtotals
        TextBox9.Text = "$0.00"
 
        If mblnResetting Then
            AllSubtotals = 0
        Else
            AllSubtotals = Total + Subtotal
        End If
        TextBox9.Text = Format(AllSubtotals, "$#,##0.00")
    End Sub
 
    Private Sub Code_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
        If mblnResetting Then
            Discount = 0
        Else
            Select Case TextBox8.Text.Trim.ToUpper
                Case "SAVE50D"
                    Discount = 50
                Case "SAVE20P"
                    Discount = 0.2 * AllSubtotals
                Case Else
                    Discount = 0
            End Select
        End If
        TextBox10.Text = Format(Discount, "$#,##0.00")
    End Sub
 
    Private Sub Tax_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged, TextBox10.TextChanged
        TextBox11.Text = "$0.00"
        If mblnResetting Then
            Tax = 0
        Else
            Const TaxRAte As Single = 0.07
            Tax = (AllSubtotals - Discount) * TaxRAte
        End If
        TextBox11.Text = Format(Tax, "$#,##0.00")
    End Sub
 
    Private Sub Total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox11.TextChanged
        TextBox12.Text = "$0.00"
        If mblnResetting Then
            GrandTotal = 0
        Else
            GrandTotal = Tax + AllSubtotals
        End If
        TextBox12.Text = Format(GrandTotal, "$#,##.00")
    End Sub
 
    Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mblnResetting = True
        For Each Ctl As Control In Me.Controls
            If TypeOf Ctl Is TextBox Then
                Ctl.Text = ""
            End If
        Next
        mblnResetting = False
    End Sub
 
End Class

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22651585
and I agree with jeffld that your controls should have significant names
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 22651626
I've been entering your code into VB and I think I see a problem.  You should consider renaming all your textbox controls to match the descriptive name.

Look at the way that you have coded TextBox9 in what you are calling button1. (your should name this cmdRecalculate or something descriptive)

The way that your data validation works is very bizarre and I would do it differently, but I'll leave that alone for now.

        Double.TryParse(Me.txtCode.Text, CDbl(Code))
        ' This makes no sense.  
        'Double.TryParse(Me.TextBox9.Text, AllSubtotals)
        'Double.TryParse(Me.TextBox9.Text, Discount)
        'Double.TryParse(Me.TextBox9.Text, Total)
 
        ' I think this is what you want.
        Double.TryParse(Me.txtAllSubTotals.Text, AllSubtotals)
        Double.TryParse(Me.txtDiscount.Text, Discount)
        Double.TryParse(Me.txtTotal.Text, Total)

Open in new window

0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 22651858
I'm also seeing a number of logic errors.  One involves an endless loop.

If the value of subtotal is changed, then it changes the value of another value which in turn changes subtotal which causes the txtSubTotal_changed event to fire again.

I think you should try to put the calculations into a button instead of using the Changed event.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22651881
>>If the value of subtotal is changed, then it changes the value of another value which in turn changes subtotal which causes the txtSubTotal_changed event to fire again.

I caught that one as well (this is my "re-entring event")
0
 

Author Comment

by:elrayiss
ID: 22655521
what about if i use the leave instead fo textchanged.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22656238
currently, as soon as you type a digit in a textbox, the other fields are instantly calculated.

if you use the Leave event, the other fields will only get calculated when you tab out of the field
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 22659928
I have these recommendations for you.

Lock fields (textboxes controls) where data is calculated programatically to prevent the users from editing those fields manually.

Perform all the calculations in a button and use data validation for every field where the user enters data.

data validation is probably the most important thing.

0
 

Author Closing Comment

by:elrayiss
ID: 31503221
very very good.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question