?
Solved

Calculate cost with tax in VB6

Posted on 2012-08-25
14
Medium Priority
?
472 Views
Last Modified: 2012-08-31
Hello all

I have 4 textbox in my form.

1-txtSousTotal.text where i put the cost. Ex: 3.58 (3.58$)
2-txtTPS.text = txtSousTotal * first tax 7.5% (3.58 * 7.5%)
3-txtTVQ.text = txtSousTotal + txtTPS * first tax 9.5% (3.58+0.27 *9.5%)
4-txtTotal.text = txtSousTotal + txtTPS + txtTVQ

But if i calculate in Excel, i would have this:
1 - 3.58
2- 0.27
3- 0.37
4- 4.21

But in my VB project, i would have this:
1 - 3.58
2- 0.27
3- 0.34
4- 4.19


So i dont have the same result.

How can i fix that?

Thanks again.

    'TPS
    txtTPS = Format(Val(txtSousTotal) * (7.5 / 100), "0.00")


    'TVQ
    txtTVQ = Format((Val(txtSousTotal) + Val(txtTPS)) * (9.5 / 100), "0.00")


    'TOTAL
    txtTPS.Text = Replace(txtTPS.Text, ",", ".")
    txtTVQ.Text = Replace(txtTVQ.Text, ",", ".")
    
    txtTotal = (Format(Val(txtSousTotal) + Val(txtTPS) + Val(txtTVQ), "0.00"))

    txtTotal = Replace(txtTotal.Text, ",", ".")

Open in new window


snapshot
0
Comment
Question by:Wilder1626
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 12

Expert Comment

by:serchlop
ID: 38333395
I think that you should use variables to make each calc and then asign they to each textbox. Maybe the diference is related to round numbers.
0
 
LVL 12

Assisted Solution

by:serchlop
serchlop earned 120 total points
ID: 38333399
Excel values are right, the problem is that. Textbox values are rounded. You should use double or real number variables to calculate the full operation and then assign they to textbox.
0
 
LVL 14

Assisted Solution

by:ThomasMcA2
ThomasMcA2 earned 120 total points
ID: 38333448
The correct value calculates a tax amount, then rounds it up or down. It does that for both tax amounts, then adds each of them to the total. The VB code adds the unrounded tax amounts to the total, and rounds once. In order to use your code, you would need to use Format to round each tax amount, val to convert each one back to a number, and then add the 3 numbers.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 50

Expert Comment

by:Martin Liss
ID: 38333460
If you calculate it correctly in VB6 you will get the same result as in Excel (.37)

txtSousTotal.Text = "3.58"
txtTPS.Text = txtSousTotal * 0.075
txtTVQ.Text = Format((Val(txtSousTotal.Text) + Val(txtTPS.Text)) * 0.095, "0.00")
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38334100
I all

I will try the variable to see if this will work.

MartinLiss, i tried like this but i have the same result as before:
    'TPS
    txtTPS.Text = Val(txtSousTotal) * 0.075

    'TVQ
     txtTVQ.Text = (Val(txtSousTotal) + Val(txtTPS)) * 0.095

    'TOTAL
    txtTPS.Text = Replace(txtTPS.Text, ",", ".")
    txtTVQ.Text = Replace(txtTVQ.Text, ",", ".")
    
    txtTotal = (Format(Val(txtSousTotal) + Val(txtTPS) + Val(txtTVQ), "0.00"))

    txtTotal = Replace(txtTotal.Text, ",", ".")

Open in new window



But if i calculate in Excel, i would have this:
1 - 3.58
2- 0.27
3- 0.37
4- 4.21

But in my VB project, i would have this:
1 - 3.58
2- 0.27
3- 0.34
4- 4.19
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38334118
OK,
I have tried with the variant like this:
Dim i As Variant
Dim j As Variant
    'TPS
     i = Val(txtSousTotal) * 0.075
      txtTPS.Text = Replace(txtTPS.Text, ",", ".")
    'TVQ
     j = (Val(txtSousTotal) + Val(txtTPS)) * 0.095
       txtTVQ.Text = Replace(txtTVQ.Text, ",", ".")

    'TOTAL
       
    txtTPS = i
    txtTVQ = j

    txtTotal = (Format(Val(txtSousTotal) + i + j, "0.00"))
    txtTotal = Replace(txtTotal.Text, ",", ".")

Open in new window


Now, it will work only after i click 2 times in the command button.

The first time i have the result:
1 - 3.58
2- 0.27
3- 0.34
4- 4.19

and the second click, i have the good result:

How can i have the good result on the first click?

hummm!!!
calculate-cost--tax-2.jpg
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38334132
All I can do is show you a picture of my results.
Result is .37
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38334147
I was able to fix it:

Dim i As Variant
Dim j As Variant
    'TPS
     i = Val(txtSousTotal.Text) * 0.075
      
    'TVQ
     j = (Val(txtSousTotal.Text) + Val(txtTPS.Text)) * 0.095

    'TOTAL
    txtTPS.Text = i
    
    txtTPS.Text = Replace(txtTPS.Text, ",", ".")
    txtTVQ.Text = Replace(txtTVQ.Text, ",", ".")
    
    j = (Val(txtSousTotal.Text) + Val(txtTPS.Text)) * 0.095
    txtTVQ.Text = j
 
    txtTotal.Text = (Format(Val(txtSousTotal.Text) + i + j, "0.00"))
    txtTotal.Text = Replace(txtTotal.Text, ",", ".")

Open in new window


I will do some test, just to make sure
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38334677
I have an idea about what is causing you to get a different answer than me.

From the looks of your code where you replace commas with periods, I assume that your Locale setting is not US English. In that case you should probably never use Val since Val is not locale-aware and if any of your textboxes has a comma in it when you use Val, the value will be truncated. For example 1,234 will be truncated to 1.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38334880
Hi MartinLiss

You are right. I'm in French Canadian setting.
0
 
LVL 50

Accepted Solution

by:
Martin Liss earned 1760 total points
ID: 38334894
I think CDbl() is locale-aware so try this.


txtSousTotal.Text = "3.58"
txtTPS.Text = txtSousTotal * 0.075
txtTVQ.Text = Format((CDbl(txtSousTotal.Text) + CDbl(txtTPS.Text)) * 0.095, "0.00")
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38352685
Are you still having problems?
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38356282
Really sorry for the delay. It was a crazy week.

Yes, all good now.

Thanks again
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38356288
You're welcome and I'm glad I was able to help.

My profile contains links to some articles I've written that may interest you.
Marty - MVP 2009 to 2012
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 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