Results 1 to 10 of 10
  1. #1
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9

    How to add a Total text box that sums a number of text boxes on a form

    Hi,

    I have a number of text boxes on a form that are updated by users. I want to have a total text box on the form that updates as amounts are entered into the other text boxes. What and where would I include the code to do this? The format of all the text boxes is standard with 2 decimal places.

    Thanks
    Jimmy

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Are you talking about having a TotalsField for each Record, i.e.

    TotalsField = Field1 + Field2 + Field3

    or a Field for totaling a Single Field on all Records, such as in a Datasheet Subform used for the Item Details of an order Form?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    It is a form to add a new record and the record has 3 fields that include amounts. I want to add a box to the form that adds up the values in the 3 text boxes as they are being updated. I don't necessarily need to save the total to the record.

    Thanks

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Assuming that the names of the three Controls are

    Textbox1
    Textbox2
    Textbox3


    In Form Design View

    1. Select the target Textbox for the totals
    2. Go to Properties - Data
    3. In the Control Source Property enter the expression below

    Code:
    =Nz([Textbox1], 0) + Nz([Textbox2], 0) + Nz([Textbox3], 0)

    Alternatively, you could:

    In the AfterUpdate event of each of the three Textboxes, enter this code, where the 4th Textbox is named TotalsTextbox:

    Code:
    TotalsTextbox = Nz(Me.Textbox1, 0) + Nz(Me.Textbox2, 0) + Nz(Me.Textbox3, 0)

    The advantage of the second method is that if, and I do mean if, you had a valid reason for storing the Totals in the underlying Table, you could do so, because the Control Source would be empty, allowing the Textbox to be Bound to a Field.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    Thanks Linq,

    I tried your first method and that worked fine and I tried the second method also to know how that could work too but when I input numbers into the text boxes that I am adding up I get the following error as soon as I move off the first text box.

    "The Expression AfterUpdate you entered as the event property setting produced the following error: Return without GoSub"

    Under the more detail section it said the following

    " This error occurs when an event has failed to run because the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name is expected to run when the event occurs."

    Do you know what I could be doing wrong?

    Thanks
    Jimmy

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    I'm not sure! Noticed that I cut off a bit in copying and pasting; should have been:

    Code:
    Me.TotalsTextbox = Nz(Me.Textbox1, 0) + Nz(Me.Textbox2, 0) + Nz(Me.Textbox3, 0)
    If that doesn't fix it copy and past the entire AfterUpdate Sub of the first Textboxinto a post.

    Also make absolutely sure that all of the Textbox names are spelled correctly.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    I didn't add the [] or the = at the beginning and everytime I delete them it just adds them back again

    The error I get now is - "Object doesn't contain the Automation object 'Me.'

    Code:
    =[Me].[InvoiceTotal]=Nz([Me].[NetFee1],0)+Nz([Me].[NetFee2],0)+Nz([Me].[NetFee3],0)

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Where are you trying to use this last bit of code? In the Control Source of InvoiceTotal or in the AfterUpdate event of Fee1, Fee2 and Fee3?

    If you look back carefully at my examples, you'll see that the syntax is different for each situation.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    I realised where I was going wrong - I was inputing it the Expression Builder of the AfterUpdate for each text box I wanted to add up. I have now moved it to the Code Builder and it is working. Thanks for your help.

  10. #10
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2013, 09:53 PM
  2. Replies: 4
    Last Post: 08-14-2012, 10:33 AM
  3. Splitting a number field across 4 text boxes
    By R_Badger in forum Reports
    Replies: 1
    Last Post: 02-06-2012, 06:12 AM
  4. Replies: 15
    Last Post: 09-18-2010, 01:19 PM
  5. Replies: 3
    Last Post: 10-23-2009, 05:03 PM

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