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.

RE: Adding values by JeanGuyMarcil

JeanGuyMarcil
Mon Jun 16 13:04:01 PDT 2008

"Ann" wrote:

> 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

RE: Adding values by gordon(dot)bentleymix(at)gmail(dot)com>

gordon(dot)bentleymix(at)gmail(dot)com>
Mon Jun 16 19:11:01 PDT 2008

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.


"Jean-Guy Marcil" wrote:

> "Ann" wrote:
>
> > 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

RE: Adding values by JeanGuyMarcil

JeanGuyMarcil
Tue Jun 17 04:44:01 PDT 2008

"Gordon Bentley-Mix" wrote:

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