[RESOLVED] Add all TextBox text to get sum
I have 13 TextBoxs aligned in a column, txtNetWeight(0) - txtNetWeight(12), yes they are indexed.
As each one loses focus I want to add all the ones before it to get a sum. Then put that sum into a TextBox named txtGrossWeight, not indexed.
I have this;
Code:
Private Sub txtNetWeight_LostFocus(Index As Integer)
Dim lngSum As Long
Dim lngAdd1 As Long
Dim lngAdd2 As Long
If txtNetWeight(Index) = "" Then
txtNetWeight(Index) = "0"
End If
For X = txtNetWeight.LBound To txtNetWeight.UBound
If txtNetWeight(X) <> "" Then
If X = 0 Then
txtGrossWeight = txtNetWeight(0)
lngSum = txtNetWeight(0)
Else
lngAdd1 = txtNetWeight(X)
lngAdd2 = lngSum
lngSum = lngAdd1 + lngAdd2
txtGrossWeight = lngSum
End If
End If
Next X
End Sub
That works but is ugly :sick: and I am sure it can be done easier. Anyone have an idea.
Thanks,
Tommy
Re: Add all TextBox text to get sum
How About
VB Code:
Dim a as Long
For a = txtNetWeight.LBound to Index
txtGrossWeight.Text = cLng(txtGrossWeight.Text) + cLng(txtNetWeight.Item(Index).Text)
Next a
Re: Add all TextBox text to get sum
Thanks for the fast reply.
I get a "Type Mismatch" error
--------------------------------
If I add a 0 in the txtGrossWeight textbox then I don't get the error but your code is coming up with some weird sums :ehh:
Re: Add all TextBox text to get sum
I would also check if current value is numeric:
Code:
Dim i As Integer
txtGrossWeight.Text = 0
For i = txtNetWeight.LBound to txtNetWeight.UBound
If IsNumeric(txtNetWeight(i).Text) Then
txtGrossWeight.Text = CLng(txtGrossWeight.Text) + CLng(txtNetWeight(i).Text))
End If
Next i
NOTE: you may want to use Format() function or FormatNumber so you would have some fixed number of decimals.
Re: Add all TextBox text to get sum
Ok something is not right here;
If I put a 1 in txtNetWeight(0) & txtNetWeight(1) & txtNetWeight(2) & txtNetWeight(3) & click in txtNetWeight(4) I get a sum of 10 in txtGrossWeight.
Re: Add all TextBox text to get sum
Quote:
Originally Posted by RhinoBull
NOTE: you may want to use Format() function or FormatNumber so you would have some fixed number of decimals.
No format needed will always be whole number
Re: Add all TextBox text to get sum
Ok, that is really up to you.
Re: Add all TextBox text to get sum
Ok if I put a "1" in txtNetWeight(0) then click in txtNetWeight(1) I get "1" in txtGrossWeight. Here is where it gets weird. If now click in any of the txtNetWeight it adds "1" to txtGrossWeight, even if txtNetWeight is empty.
If I put a "2" in txtNetWeight(0) then click in txtNetWeight(1) I get "2" in txtGrossWeight. Here is where it gets weird. If now click in any of the txtNetWeight it adds "2" to txtGrossWeight, even if txtNetWeight is empty.
Re: Add all TextBox text to get sum
You need to clear your gross text first - I edited post #4. :)
Re: Add all TextBox text to get sum
Yes I got that.
But this is not working;
Code:
txtGrossWeight.Text = CLng(txtGrossWeight.Text) + CLng(txtNetWeight(i).Text)
It is adding txtGrossWeight to itself so it is doubling every time txtNetWeight_LostFocus
I am getting there. Thanks for your help. But still a little problem. I have this now;
Code:
Dim i As Integer
For i = txtNetWeight.LBound To txtNetWeight.UBound
If IsNumeric(txtNetWeight(i).Text) Then
If i = 0 Then
txtGrossWeight.Text = CLng(txtNetWeight(i).Text)
Else
txtGrossWeight.Text = CLng(txtNetWeight(i - 1).Text) + CLng(txtNetWeight(i).Text)
End If
End If
Next i
But that only adds the one before txtNetWeight(i) not all.
Re: Add all TextBox text to get sum
Here's my quick stab at it
Code:
Dim i As Integer
Dim lngTotal As Long
lngTotal = 0
For i = txtNetWeight.LBound To txtNetWeight.UBound
If IsNumeric(txtNetWeight(i).Text) Then
lngTotal = lngTotal + CLng(txtNetWeight(i).Text)
End If
Next i
txtGrossWeight.Text = lngTotal
Re: Add all TextBox text to get sum
Quote:
Originally Posted by Tommy2007
But that only adds the one before txtNetWeight(i) not all.
Can you just copy and run sample code from post #4?
Re: Add all TextBox text to get sum
Quote:
Originally Posted by RhinoBull
You need to clear your gross text first - I edited post #4. :)
:wave: :wave: :wave:
That worked. THANKS!
I know you said that but I thought you meant to stop the "Type Mismatch" error. So I was just manually putting a "0" in txtGrossWeight at run time then trying to put numbers in txtNetWeight and see what happen. Now that I see what you are doing I get it now.
Re: [RESOLVED] Add all TextBox text to get sum
I'm glad you sorted it out.