Consulting

Results 1 to 6 of 6

Thread: Sum text box values

  1. #1
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    Sum text box values

    This seems so simple I'm almost embarressed to ask but:
    I have a form where I want a text box to display the total of the textbox values on the form.

    [vba]Private Sub Calculate()
    On Error Resume Next
    TextBox3.Value = TextBox1.Value + TextBox2.Value
    ' TextBox3.Value = (Format(TextBox, "0.00"))
    End Sub
    Private Sub TextBox1_Change()
    OnlyNumbers
    Calculate
    End Sub
    Private Sub TextBox2_Change()
    OnlyNumbers
    Calculate
    End Sub

    Private Sub OnlyNumbers()
    If TypeName(Me.ActiveControl) = "TextBox" Then
    With Me.ActiveControl
    If Not IsNumeric(.Value) And .Value <> vbNullString Then
    MsgBox "Sorry, only numbers allowed"
    .Value = vbNullString
    End If
    End With
    End If

    End Sub[/vba]
    This works when multiplying the values
    [vba]TextBox3.Value = TextBox1.Value * TextBox2.Value[/vba]
    So i must be in the right area???

    As an add on I would like the total to be rounded to be the sum of textbox1 and textbox2, divided by 1000 and dsiplayed to 2 figures

    eg

    1234 + 3456 = 4.69

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    [vba]

    TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")
    [/vba]

  3. #3
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    sub or function not defined

    XLD
    If I just copy your code:
    [vba]TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00") [/vba]

    over my line
    [vba]TextBox3.Value = TextBox1.Value + TextBox2.Value[/vba]
    I get an error:

    "Sub or function not defined."

  4. #4
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Look at the first CDbl function call (two "l"s). It's a typo.
    Glen

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Quote Originally Posted by lifeson
    XLD
    If I just copy your code:
    [vba]TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00") [/vba]
    over my line
    [vba]TextBox3.Value = TextBox1.Value + TextBox2.Value[/vba] I get an error:

    "Sub or function not defined."
    Sorry, I changed the Val to CDbl just before posting (Because of an issue with another post a few days ago), but messed it. It should be

    [vba]TextBox3.Value = Format((CDbl(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00") [/vba]

  6. #6
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Cheers
    As usual thanx for the help

Posting Permissions

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