|
-
Aug 13th, 2007, 04:08 PM
#1
Thread Starter
Junior Member
[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 and I am sure it can be done easier. Anyone have an idea.
Thanks,
Tommy
-
Aug 13th, 2007, 04:31 PM
#2
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
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Aug 13th, 2007, 04:35 PM
#3
Thread Starter
Junior Member
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
Last edited by Tommy2007; Aug 13th, 2007 at 04:41 PM.
Reason: Not Adding correctly
-
Aug 13th, 2007, 04:38 PM
#4
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.
Last edited by RhinoBull; Aug 13th, 2007 at 05:14 PM.
-
Aug 13th, 2007, 04:49 PM
#5
Thread Starter
Junior Member
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.
-
Aug 13th, 2007, 04:51 PM
#6
Thread Starter
Junior Member
Re: Add all TextBox text to get sum
 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
-
Aug 13th, 2007, 04:52 PM
#7
Re: Add all TextBox text to get sum
Ok, that is really up to you.
-
Aug 13th, 2007, 05:11 PM
#8
Thread Starter
Junior Member
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.
-
Aug 13th, 2007, 05:15 PM
#9
Re: Add all TextBox text to get sum
You need to clear your gross text first - I edited post #4.
-
Aug 13th, 2007, 05:28 PM
#10
Thread Starter
Junior Member
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.
-
Aug 13th, 2007, 06:43 PM
#11
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
-
Aug 13th, 2007, 07:47 PM
#12
Re: Add all TextBox text to get sum
 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?
-
Aug 13th, 2007, 09:25 PM
#13
Thread Starter
Junior Member
Re: Add all TextBox text to get sum
-
Aug 14th, 2007, 12:39 PM
#14
Re: [RESOLVED] Add all TextBox text to get sum
I'm glad you sorted it out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|