Adding and multiplying multiple combo and text boxes

Artemis Hothmire

Registered User.
Local time
Today, 07:55
Joined
Nov 19, 2012
Messages
23
Good morning all,

I am building a form that multiply's the choosen value of one combo box to a set price in a text box. I want this product then to be added to four more like opperations and have the final value entered into a solitary text box. Also, some opperations dont have to be chosen and will remain null in most cases, as the customer has the option of up to five assets to buy. I believe that this is what is messing with my syntax. Could one of you help me with this? It would be greatly appreciated.
As of right now im doing this....:banghead:

-Artemis
 
If you have a limited number of items to perform your arithmetic operations, the simple way is just to do something like this:
Code:
Dim Result as Double

If Not IsNull(cbo1) Then
Result = cbo1
Else
Result = 0

If Not IsNull(txt2) Then
Result = Result + txt2
End If

If Not IsNull(txt3) Then
Result  = Result/txt3
End If

If Not IsNull(txt4) Then
Result = Result - txt4
End If

...
This is just air code, and you will obviously have to adapt it to your needs, such as control names, operations or the value for Results on Null values. Also make sure to have some code to check if you're dividing by 0.

Edit: Think I misinterpreted what you meant. I'm guessing you have fields like:
Price 1, Quantity1, Price2, Quantity2, ..., PriceN, QuantityN
and you want to do something like:
Price1 * Quantity1 + Price2 * Quantity2 + ... + PriceN * QuantityN

If that's the case, it might look something like:
Code:
Dim Result as Double
Result = 0

'Repeat for each set of products
If Not IsNull(Quantity1) And Not IsNull(Price1) Then
Result = Result + (Quantity1*Price1)
End If

txtResult = Result
Edit2: For something a bit more robust or at least neater looking, you can create a Function such as:
Code:
Function GetProduct(value1 As Variant, value2 As Variant)
    GetProduct = nz(value1, 0) * nz(value2, 0)
End Function

Then if you type in GetProduct(price1*quantity1), it will return the product of the two values you passed or 0 if either/both are Null.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom