Adding values

A

Ann

How do I add the values of a textbox. I have a User Form with four textboxes:

Text1, text2, text3 and text4.

text1 can not be greater then the sum of text2, text3 and text4.
 
J

Jean-Guy Marcil

Ann said:
How do I add the values of a textbox. I have a User Form with four textboxes:

Text1, text2, text3 and text4.

text1 can not be greater then the sum of text2, text3 and text4.

If the code is part of the userform code pane:

If you are not handling decimals:

With Me
If CLng(.Text1) > (CLng(.Text2)+CLng(.Text3)+CLng(.Text4))
Msgbox "Text 1 cannot be greated than the sum of the other three
fields."
Else
Msgbox "Text 1 is smaller than the sum of the other three"
End If
End With

If you are handling decimals:

With Me
If CSng(.Text1) > (CSng(.Text2)+CSng(.Text3)+CSng(.Text4))
Msgbox "Text 1 cannot be greated than the sum of the other three
fields."
Else
Msgbox "Text 1 is smaller than the sum of the other three"
End If
End With
 
G

Gordon Bentley-Mix

Ann,

Be aware that the CLng/CSng functions return zero for non-numeric values, so
your results may not be as expected if one of your TextBoxes has letters in
it. Accordingly, you may want to consider some validation on the individual
TextBox values - possibly on the Exit or Change events of the TextBoxes -
before doing this comparison.

In addition, it is good coding practice to specify the property of the
control being evaluated explicitly, rather than just relying on the default.
So in Jean-Guy's code, for example, you would use:

With Me
If CLng(.Text1.Value) >
(CLng(.Text2.Value)+CLng(.Text3.Value)+CLng(.Text4.Value))
etc...
End With
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

Jean-Guy Marcil

Gordon Bentley-Mix said:
In addition, it is good coding practice to specify the property of the
control being evaluated explicitly, rather than just relying on the default.

Very true!
I usually include the appropriate property... I guess this is what hapens
when you write too fast... I was a bit of in a hurry to leave the office...

Thanks for catching that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top