Results 1 to 10 of 10
  1. #1
    qwerty is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    91

    Adding sum of multiple textboxes

    I have multiple text boxes, and a text box to display the sum, I have tried this

    Sub GetSumOfTbs()
    Me.Textbox3 = (IIf(Me.Textbox1 = "", 0, Me.Textbox1) + 0) + (IIf(Me.Textbox2 = "", 0, Me.Textbox1) + 0)
    End Sub

    It doesn't do anything at all, when I type in a number in the field.

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Your textboxes likely are null not empty strings.
    Instead use the Nz function for each control e.g. Nz(Me.Textbox1,0)

    BTW You also made a mistake in the second part of your code. Can you see what it is?
    Last edited by isladogs; 12-02-2018 at 02:47 PM. Reason: Extra sentence
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Are you trying to save calculated value - why?

    Could just have expression in Textbox3 ControlSource. And why are you adding 0?

    =Nz(Textbox1,0) + Nz(Textbox2,0)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    qwerty is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    91
    Nothing is happening ??????

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You type a number into a textbox? Then do you press Enter or Tab or click somewhere else?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    qwerty is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    91
    I am trying to get it, so if enter a number in textbox1. Then textbox3 will update, no button or anything. What is your recommendation?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Recommendation already made.

    1. expression in textbox3 ControlSource

    2. type value into textbox1 or textbox2 then press Enter or Tab or click into another control - focus must leave textbox1 or textbox2.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    qwerty is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    91
    Quote Originally Posted by June7 View Post
    Recommendation already made.

    1. expression in textbox3 ControlSource

    2. type value into textbox1 or textbox2 then press Enter or Tab or click into another control - focus must leave textbox1 or textbox2.
    Thank you, but when type 1 and 2 it equals to 12? suppose to be 3

  9. #9
    qwerty is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    91
    It is working now...thank you

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Are textbox1 and textbox2 bound to TEXT type fields?

    + sign is concatenation character left over from old BASIC. If both fields are text type then they will concatenate instead of sum.

    Either change the field type or:

    =Int(Nz(Textbox1,0)) + Int(Nz(Textbox2,0))

    or

    =Val(Textbox1,0)) + Val(Nz(Textbox2,0))

    or

    =CDbl(Nz(Textbox1,0)) + CDbl(Nz(Textbox2,0))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 11-28-2018, 04:06 PM
  2. Replies: 3
    Last Post: 05-03-2017, 12:46 PM
  3. Adding Multiple Records
    By tezy1994 in forum Access
    Replies: 8
    Last Post: 11-26-2013, 09:57 PM
  4. Replies: 7
    Last Post: 06-04-2013, 01:10 PM
  5. Set Listbox Filter w/ Multiple Textboxes
    By Ganymede in forum Forms
    Replies: 1
    Last Post: 01-13-2012, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums