Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a pre-accounting application on vb and my code is like this.

Private Sub txtAmount_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim vatprice As Double
Dim price As Double
If KeyCode = 13 Then
vatprice = txtAmount.Value * (txtOnePrice.Value * txtVAT.Value / 100)
price = txtAmount.Value * txtOnePrice.Value
txtTotal.Value = price + vatprice
End If
End Sub


But when try to enter my one price and put my VAT value and than put how many of the items should i buy it gives me the wrong answer.

txtOnePrice = 8000
txtVAT = 0.08
txtAmount = 1
TOTAL = 8006

Can someone please help me?

What I have tried:

i have tried these combinations

(txtOnePrice.Value * txtVAT.Value / 100) * txtAmount.Value 


((txtOnePrice.Value * txtVAT.Value) / 100) * txtAmount.Value


(txtOnePrice.Value * (txtVAT.Value / 100)) * txtAmount.Value


but none of them worked and somehow they all gave the same answer too. Its weird
Posted
Updated 14-Dec-20 7:44am
Comments
BabyYoda 14-Dec-20 10:11am    
VAT is already in a percentage form, isn't it? 0.08 is 8%? So, you are dividing by 100 again making it 0.0008.

But all you have to do is debug the code. You can put a breakpoint in the code and see exactly what is happening.
mekenix 14-Dec-20 10:14am    
well sir you won the lottery :D i am a dummy


Your result is correct based on the values and formula provided. Perhaps you meant to enter a VAT rate of 8% instead of 0.08%?
vatprice = txtAmount.Value * (txtOnePrice.Value * txtVAT.Value / 100)
         = 1               * (8000              * 0.08         / 100)
         = 1               * (8000              * 0.0008)
         = 1               * 6.4
         = 6.4
 
Share this answer
 
Comments
Richard Deeming 14-Dec-20 10:20am    
If removing / 100 from that code and entering the same values still produces 8006 instead of 8640, then either that's not the code that's running, or you haven't recompiled.
In logical terms you should have something like this:
NetTotal = NumberOfItems * UnitPrice   ' e.g. 5 * 8000
VatTotal = NetTotal * (VatRate / 100)  ' Assuming VatRate is a percentage number, eg. 8.0, 17.5 etc.
GrossTotal = NetTotal + VatTotal       ' the final
 
Share this answer
 
Quote:
Can someone please help me?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging in Excel VBA - EASY Excel Macros[^]
MS Excel 2013: VBA Debugging Introduction[^]
How to debug Excel VBA - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
As mentioned in the comments, you are dividing a number that is already in percentage format by 100.
 
Share this answer
 
i solved it by making it like
vatprice = txtAmount.Value * (txtOnePrice.Value * txtVAT.Value)


this.

Thanks everybody
 
Share this answer
 
Comments
Richard Deeming 15-Dec-20 4:18am    
So exactly what I already suggested in Solution 1, which "did not solve it"?

And also suggested in the comments, and solutions 2 and 3.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900