Userform

S

Silsila

Hi All,

Can I add up in a text box in userform. For example the
user would type 10+12.50 in the text box and the text box
would show 22.50

Also how can I add up few text boxes into another one. Any
help is appreciated. Thanks in advance.

Regards
 
R

Rob van Gelder

Private Sub TextBox1_AfterUpdate()
TextBox1.Text = Evaluate(TextBox1.Text)
End Sub

If you wanted to post the result into a different Textbox, say, TextBox2:
TextBox2.Text = Evaluate(TextBox1.Text)
 
S

Silsila

Hi Rob,

Thank you for your reply. The first code to sum up in the
textbox works fine. Thank you again.

The second code i tried as
TextBox3.Text = Evaluate(TextBox1.Text) + Evaluate
(TextBox2.Text)

does not work when either textbox1 or textbox2 is empty.

Thanks for your help.

Regards
 
S

Stevie_mac

Dude, there is many ways to handle this sort of thing. This realy is basic stuff!
You say "does not work when either textbox1 or textbox2 is empty." - Check it!

The example you were given was a point in the right direction, you gotta check things
and handle exceptions like "textbox is empty", this is what you have to do to account for
people who try... "I wonder what will happen if i try to calculate..." "so and so + is a w@nker"
You know what i mean!

Sub DoMath()
dim r1, r2
on error goto FAIL
if TextBox1.Text = "" then
r1 = 0
else
r1 = Evaluate(TextBox1.Text)
End if

if TextBox2.Text = "" then
r2 = 0
else
r2 = Evaluate(TextBox3.Text)
End if

TextBox3.Text = r1 + r2

Exit Sub
FAIL:
msgbox "An error occured. Check your input"
End Sub
 
S

Stevie_mac

Man - dont you just hate it when you spot an error in yer own stuff!!!

Sub DoMath()

Dim r1, r2
On Error GoTo FAIL
If TextBox1.Text = "" Then
r1 = 0
Else
r1 = Evaluate(TextBox1.Text)
End If

If TextBox2.Text = "" Then
r2 = 0
Else
'''r2 = Evaluate(TextBox3.Text) ' <<<< incorrect - DOH
r2 = Evaluate(TextBox2.Text) ' <<<< Corerect :)
End If

TextBox3.Text = r1 + r2

Exit Sub
FAIL:
MsgBox "An error occured. Check your input"

End Sub
 
S

Silsila

Hi Stevie_mac,

Thank you for your help. Yes, eventually I will get to the
stage when I will feel comfortable exploring. Thanks.

Regards
 
T

Trev

specially when you've been a bit on the short side with
the questioner.
Regards Trev
 
Top