PDA

Click to See Complete Forum and Search --> : Textbox Total of Other Textboxes on a Form [RESOLVED]


PineyWoodsJimbo
Feb 10th, 2004, 02:21 PM
Please tell me if I am on the right track.

I have a userform with many textboxes, but let's just say there are three. I want the third textbox to always show a running total of the amounts in the other boxes on the form. I have tried some of the suggestions in other threads without success. Also, if if is easier to create an array of control objects (textboxes), how do I do that?

Here is what I have so far:

Private Sub AddAll_Click()
Dim BoxArray As Integer
Dim TotalSum As Double
TotalSum = 0
For BoxArray = 1 To 2
TotalSum = TotalSum + Val(Values(BoxArray).Text)
Next BoxArray
UserForm1.TextBox3.Value = TotalSum
End Sub


Thanks,
Jimbo.
:eek:

Shattered
Feb 11th, 2004, 03:42 AM
Sub Count_text()
Dim dTotal As Double
dTotal = 0
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "TextBox" Then
If ctl.Name <> "txtTotal" Then
dTotal = dTotal + Val(ctl.Value)
End If
End If
Next ctl
txtTotal = dTotal
End Sub

This does rely on you renaming your total text box however with the name txtTotal (or amending the code to reflect the name of teh text box that contains the total). This will add up all the text boxes on the form, no matter how many you add.

PineyWoodsJimbo
Feb 18th, 2004, 11:01 AM
Shattered-

Much thanks for the excellent code. I really appreciate the help.

Jimbo.

It ended up looking like this in my test form:

___________________________________________
Sub Count_text()
'Adds the values in the other textboxes together and
'makes the total visible in the last textbox (txtTotal)

Dim dTotal As Double
dTotal = 0
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "TextBox" Then
If ctl.Name <> "txtTotal" Then
dTotal = dTotal + Val(ctl.Value)
End If
End If
Next ctl
txtTotal.Text = dTotal

End Sub
__________________________________________
Private Sub Addit_Change()
'Addit is the name of textbox1
Count_text

End Sub
___________________________________________
Private Sub Adds_Change()
'Adds is the name of textbox2
Count_text

End Sub