Results 1 to 5 of 5
  1. #1
    alscompany is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    2

    Calculate a field in a form

    Hi all, I'm trying to use two values to calculate a field on a form. When I use code below in the AfterUpdate in the fields KPI1 and KPI2

    Code:
    Me.Score = (Me.KPI1.Value + Me.KPI2.Value) / (Me.TouchesTotal.Value * 2)
    it doesn't calculate properly, for example when the values above are 5, 5 and 5, I should get 100%, however, I get 5.5, when I use values 4,4 and 5, I should get 80%, but I get 4.4. When I use the above statement with SUM in front, I get SUB or FUNCTION not defined error. I've tried using the control source of the field and that doesn't display anything. Thanks in advance for any help provided.

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Have you tried using the same code in the LostFocus event?

  3. #3
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Sorry - I just tried what you described and realized the error that I was getting.
    I got the same results as you.
    Then I put a break point in the code and realized that the values in my text boxes were coming out as "5", "5", and "5" - instead of 5, 5, and 5.

    I changed my AfterUpdate event to this:
    Code:
    Private Sub Text3_AfterUpdate()
    Dim intText1, inText2, inText3 As Integer
    
    intText1 = CInt(Me.Text1.Value)
    intText2 = CInt(Me.Text2.Value)
    intText3 = CInt(Me.Text3.Value)
    
    Me.Text4 = ((intText1 + intText2) / (intText3 * 2)) * 100
    
    End Sub
    . . . and I got the correct percentage.

    Let me know if this works for you.

  4. #4
    alscompany is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    2
    worked like a charm! thanks a bunch Robeen.

  5. #5
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Happy to help.
    Mark the thread as solved if you're done with it!

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

Similar Threads

  1. Replies: 7
    Last Post: 05-02-2013, 11:14 PM
  2. Replies: 5
    Last Post: 03-06-2011, 10:38 AM
  3. Replies: 5
    Last Post: 12-17-2010, 11:06 AM
  4. Replies: 1
    Last Post: 12-10-2010, 11:03 AM
  5. Calculate one field based on another one
    By Douglasrac in forum Forms
    Replies: 7
    Last Post: 11-18-2010, 01:30 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