elrayiss
asked on
help with code
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
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)
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)
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
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
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.
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and I agree with jeffld that your controls should have significant names
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.
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)
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.
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.
>>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")
I caught that one as well (this is my "re-entring event")
ASKER
what about if i use the leave instead fo textchanged.
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
if you use the Leave event, the other fields will only get calculated when you tab out of the field
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.
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.
ASKER
very very good.
>>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.