Results 1 to 3 of 3
  1. #1
    rgrogan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2013
    Posts
    2

    Question Create a query with a derived field based on each change in recordset

    I need to come up with a way to derive field X (see below) in a query.

    For each change in field A, Set X=1
    For each change in field B, X=X+1

    The below table shows what the query results should look like. Thanks for your help!



    A
    X
    B
    A14-040 1 971
    A23-020 1 676
    A35-032 1 300
    A35-032 2 776
    A35-032 2 776
    A35-032 2 776
    A35-032 2 776
    A35-032 2 776
    A35-032 2 776
    A35-032 3 500
    B45-031 1 776
    B45-031 1 776
    C27-010 1 776
    C27-010 2 999
    C27-010 2 999

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Calculations dependent on values in another record of same table are difficult. You essentially want to assign a value to record groups. Review: http://allenbrowne.com/ranking.html
    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
    rgrogan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2013
    Posts
    2

    Smile

    Quote Originally Posted by June7 View Post
    Calculations dependent on values in another record of same table are difficult. You essentially want to assign a value to record groups. Review: http://allenbrowne.com/ranking.html
    Thanks! I had someone on another forum respond with a solution (Below). Thanks for you help!
    Code:
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim holdA, holdB, Hold
    XstrSQL = "SELECT A, X, B FROM YourTable ORDER BY A, B"
    Set rs = CurrentDB.OpenRecordset( strSQL, dbOpenDynaset)
    holdA = ""
    holdB = ""
    holdX = 0
    Do Until rs.EOF     
    rs.Edit     
    If rs!A <> holdA Then           
       holdX = 1     
    ElseIf  rs!B <> holdB Then          
       holdX = holdX  + 1     
    End If 
    rs!X = holdX     
    holdA = rs!A 
    holdB = rs!B     
    rs.Update     
    rs.MoveNext
    Loop
    Last edited by June7; 05-05-2013 at 06:51 PM. Reason: fix code for readability

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

Similar Threads

  1. Change field color based on result
    By dniezby in forum Programming
    Replies: 1
    Last Post: 05-01-2013, 09:43 PM
  2. Change Value Based off Value in Field
    By jo15765 in forum Queries
    Replies: 3
    Last Post: 04-18-2013, 08:19 AM
  3. creating recordset based on query
    By akrylik in forum Access
    Replies: 8
    Last Post: 05-10-2012, 02:57 PM
  4. Replies: 1
    Last Post: 02-29-2012, 10:13 PM
  5. Replies: 11
    Last Post: 12-04-2010, 10:20 AM

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