Results 1 to 14 of 14

Thread: [RESOLVED] Add all TextBox text to get sum

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    Resolved [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

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Add all TextBox text to get sum

    How About

    VB Code:
    1. Dim a as Long
    2.  
    3. For a = txtNetWeight.LBound to Index
    4.      txtGrossWeight.Text = cLng(txtGrossWeight.Text) + cLng(txtNetWeight.Item(Index).Text)
    5. 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."


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    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

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    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

  7. #7

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    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.

  9. #9

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    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.

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  12. #12

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    23

    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.

    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.

  14. #14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width