Solved

Calculating

Posted on 2002-03-26
2
236 Views
Last Modified: 2010-05-02
I would like to know how i can calculate the total for each product.
i have 4 textboxes that are control arrays

txtUnitPrice(Index)
txtQuantity(Index)
txtDiscount(Index)
txtTotal(Index)

i tried this

txtTotal(Index) = CDbl(txtUnitPrice(Index)) * CDbl(txtQuantity(Index)) / CDbl(txtDiscount(Index))

it doesn't seem to work

where would be the best place to put this code so that the total is
automatically calculated?
0
Comment
Question by:digitaldestruction66
2 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 6897827
I'd put it into a subroutine that's called by the Change event (or maybe the LostFocus event) of each of the textboxes:

private sub txtUnitPrice_Change(Index as integer)
   UpdateTotal Index
end sub

private sub txtQuantity_Change(Index as integer)
   UpdateTotal Index
end sub

...

private sub UpdateTotal(Index as integer)
  ' note that ".text" was added to each to make it .Net-compliant...it's also good practice
  txtTotal(Index).text = CDbl(txtUnitPrice(Index).text) * CDbl(txtQuantity(Index).text) / CDbl(txtDiscount(Index).text)
end sub
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 6898143
Dear digitaldestruction66,

You can put this code in either Change event, LostFocus event or in Validate event of the text box. In the Change event your value will be calculated if you cange anything in the textbox where as in LostFocus event the calculation will be done when the focus is lost from any of the textbox. In case of Validate event, which fires just before the LostFocus event, you can calculate your value too. The difference between LostFocus and Validate event is that in Validate event you can perform some validations and if the entered values are valid then only calculate the value and permit the cursor to move to the next textbox other restrict the cursor to the same textbox by setting the Cancel=True (The LostFocus event does not have the Cancel parameter!!).

I hope this will help you.

Thanks,
KarcOrigin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now