Results 1 to 8 of 8
  1. #1
    gaya123 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    3

    Question How to calculate a field value dynamically in a table

    I have two tables. Product table has "product price" column. OrderLine table has "quantity ordered" column. I have a column "LineItemCost" in OrderLine table. The value for line item cost is (product price*quantity ordered). This needs to be updated dynamically in the OrderLine table when each row is created. Could someone help me with this????? I am totally stuck and I am not very familiar with Access..

    Thanks in advance for the help!

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    You CAN create a Calculated field in the Table.

    To do this - both the UnitPrice and Quantity need to be in the same table.
    If they are - then - in table design - add a field and name it TotalCost - for example.
    For Data Type for this field - select 'Calculated'.
    In the calculation [expression] builder type in:
    [UnitPrice] * [Quantity]

    I think it might be better to create a query, though, and have the calculation in the query - rather than in the table.

    I hope this helps!

  3. #3
    gaya123 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    3

    I tried to create update query

    I tried to create an update query to multiply price and quantity and get line item cost. I want to know how i could invoke this query automatically when each record is created in order line table?

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    What exactly do you need to have happen when you create a new record?

    Can you explain for me what you mean when you say 'invoke this query'?
    What are you envisioning when the query is invoked?

  5. #5
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Having calculated fields in tables is a bad database practice. You should only do calculations in queries and forms. If you need the calculated information it can easily be retrieved in this manner.

    More information on this

    http://www.access-programmers.co.uk/...d.php?t=196906

  6. #6
    gaya123 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    3
    The value for lineitemcost column needs to be updated dynamically every time a record is inserted in order line table.

  7. #7
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    If you use that calculated field in the Table - which is not the best thing to do - then as soon as a new record is inserted [committed] into the table - the Calculated field will become available.

    Again - this is not 'best practice'.

  8. #8
    shafiq037 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    3
    it works good for number. but dates can not be calculated to find age of a person in this way.

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

Similar Threads

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