During input calculation to get sum value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sophannaly
    New Member
    • Mar 2014
    • 67

    #1

    During input calculation to get sum value

    Hi,

    In my continuous form, I have few textbox fields like:

    Sum amount1 amount2 amount3

    Sum is the value that user can't change. User need to input value for amount1, amount2, and amount3. But these three values summation can't exceed Sum value.

    Can anybody point me out the good way to go this?

    Best regards,
    Sophanna
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Sophannaly,
    In the BeforeUpdate event of each amount text box put this:
    Code:
    if nz(me.aount1,0)+nz(me.amount2,0)+nz(me.amount3,0)>me.Sum then docmd.cancelevent
    That will prevent any entry that makes the total go over Sum. You might want to add a message box to tell what is happening.

    Jim

    Comment

    • sophannaly
      New Member
      • Mar 2014
      • 67

      #3
      Hi Jim,

      Thanks you so much for your code.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Rather than using DoCmd.CancelEve nt, you can simply set the the defined variable [Cancel] to True. This is how BeforeUpdate events are designed to work.

        Both methods will work, of course, but using [Cancel] ensures all the code is explicitly working on the current object.

        Comment

        • sophannaly
          New Member
          • Mar 2014
          • 67

          #5
          Hi NeoPa,

          Thanks you so much and before I used to wonder what Cancel variable use for and now I know it from you.

          Sophanna

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            More than happy to help Sophanna :-)

            Comment

            • jimatqsi
              Moderator Top Contributor
              • Oct 2006
              • 1293

              #7
              Thanks for the tip, NP!

              Jim

              Comment

              Working...