Solved

Calculating

Posted on 2002-03-26
2
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using "ScreenUpdating" 6 84
Spell Check in VB6 13 138
Error with a code discussed on this page 5 39
how to loop through and process two columns in excel 8 54
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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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