Results 1 to 8 of 8
  1. #1
    jviel33 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Location
    Massachusetts
    Posts
    2

    How to iterate a calculation across values within an Access table

    Hello everyone,

    I have created a table in Microsoft Access 2013 that contains pricing information. The first column of the table contains an ID number populated by Access, the second column contains a short-text name "Node" and the third column contains a number "Price".

    My ultimate goal is to iterate across all values within the second column "Node" of the table, holding one constant at a time to calculate the difference between two nodes and create a new table with these calculated values. I have experience writing VBAs in Excel, but I am having a difficult time finding a good source to get started. Any help would be much appreciated.

    (Current Table)

    ID Node PriceA
    1 A 10
    2 B 15
    3 C 20


    (Desired Macro Calculation)

    Node Node Price
    A A 0
    A B -5
    A C -10
    B A 5
    B B 0


    ... ... ...


    Thank you in advanced,
    Jeff

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    This would require lots of VBA code manipulating recordsets within looping structure and writing records to table.
    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.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I agree with June. In fact I think Excel is more suited to your requirement -especially since you have experience with Excel.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    I think this would be pretty easy you can try this and tell me if it is what you want or not:

    Assume your dataset as you've explained it is called tblTest

    Create this query
    Code:
    SELECT tblTest.node AS RowNode, tblTest.pricea AS RowPrice, tblTest_1.node AS ColNode, tblTest_1.pricea AS ColPrice
    FROM tblTest, tblTest AS tblTest_1;
    Call this query qryCartesian

    then create this query:
    Code:
    TRANSFORM Sum([rowprice]-[colprice]) AS Expr1
    SELECT qryCartesian.RowNode
    FROM qryCartesian
    GROUP BY qryCartesian.RowNode
    PIVOT qryCartesian.ColNode;
    this should give you a matrix showing you what you want (I assumed you wanted a matrix).

    If you don't want the matrix you can do this with a single query:

    Code:
    SELECT tblTest.node AS RowNode, tblTest.pricea AS RowPrice, tblTest_1.node AS ColNode, tblTest_1.pricea AS ColPrice, [rowprice]-[colprice] AS Difference
    FROM tblTest, tblTest AS tblTest_1;

  5. #5
    jviel33 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Location
    Massachusetts
    Posts
    2
    Thanks rpeare! This is exactly what I was looking for

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Rats! How embarrassing, I should have remembered Cartesian relation. Glad you have a working solution.
    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.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    it's only fair june, I don't know how many times you've caught stuff I've missed.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I think you should get some sort of guessing prize for deciphering (correctly) what the OP was thinking.

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

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2014, 12:53 AM
  2. Calculation Field in table vs Query calculation
    By Leonidsg in forum Database Design
    Replies: 18
    Last Post: 02-28-2013, 09:38 PM
  3. Replies: 1
    Last Post: 12-18-2012, 03:59 PM
  4. How to iterate through a custom collection
    By vicrauch in forum Programming
    Replies: 6
    Last Post: 07-21-2011, 02:51 PM
  5. Iterate through records and print report
    By cap10101 in forum Programming
    Replies: 5
    Last Post: 11-25-2010, 04:09 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