Solved

Calculating

Posted on 2002-03-26
2
228 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…

747 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

14 Experts available now in Live!

Get 1:1 Help Now