Solved

# Calculating

Posted on 2002-03-26
228 Views
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
Question by:digitaldestruction66

LVL 22

Accepted Solution

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

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!!).

Thanks,
KarcOrigin
0

## Featured Post

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…