Sum text boxes

I have a user interface that contains 10 text boxes. I need to create a loop which goes through the text box values and ensure that it below 100% weight.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Glenn RayExcel VBA DeveloperCommented:
If you rename your text boxes so that they contain an ordinal number in them, you should be able to write code that loops through all of them.

For example, if you label them "txt1", "txt2", "txt3" and so on, you could use code like so:

Code within the form:
'sum text boxes
intSum = 0
For c = 1 To 10
     intSum = intSum + Me.Controls("txt" & c).Value
Next c

Open in new window

Code external to the form:

'replace frmName with your form's actual name
intsum = 0
For c = 1 To 10
    intsum = intsum + frmName.Controls("txt" & c).Value
Next c

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Give this a try in your VBA code window:
sub subboxes
Dim num(1 To 3) As Integer
num(1) = TextBox1.Value
num(2) = TextBox2.Value
num(3) = TextBox3.Value
[a1] = num(1) + num(2) + num(3)
end sub

Open in new window


Well, I am not sure how you name your text box.
You could check the 1st code you know the name.
or you can try the 2nd code to add the value on all the text box
Private Sub CommandButton1_Click()
Dim abc As Double

abc = CDbl(Me.TextBox1.Value) + CDbl(Me.TextBox2.Value)
Me.Label1.Caption = abc

End Sub

Open in new window

Private Sub CommandButton1_Click()
Dim abc As Double
'abc = CDbl(Me.TextBox1.Value) + CDbl(Me.TextBox2.Value)
abc = 0

Dim cCont As Control

    For Each cCont In Me.Controls

        If TypeName(cCont) = "TextBox" Then

            abc = abc + cCont.Value

        End If

     Next cCont

Me.Label1.Caption = abc

End Sub

Open in new window

If you only have 10 text boxes, why bother with a loop.  Why not just hard-code the 10 text box names once and be done with it.
Trying_to_LearnAuthor Commented:
Thanks Guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.