Results 1 to 9 of 9
  1. #1
    vishv.kemonte is offline Novice
    Windows 10 Access 2021
    Join Date
    Jan 2024
    Posts
    6

    accessing the previous line in the query

    Hi, everyone!There is a table with two columns
    ст1 ст2
    Кон1 1
    Кон1 2
    Кон1 5
    Кон1 6
    Кон1 9
    Кон2 1
    Кон2 2
    Кон2 5
    Кон2 6
    Кон2 9



    I want to make groups in the query.The condition for creating a group is simple: sorting by column 1 and column 2. If the value of column 1 differs from the previous value of column 1, then the group number increases by 1 compared to the group of the previous row. Also, if the value of column 2 is greater than the previous value of column 2 by more than 1, then the group number is also increased by 1 compared to the group of the previous row. As a result, you will get the following:

    ст1 ст2 Gr
    Кон1 1 1
    Кон1 2 1
    Кон1 5 2
    Кон1 6 2
    Кон1 9 3
    Кон2 1 4
    Кон2 2 4
    Кон2 5 5
    Кон2 6 5
    Кон2 9 6

    Is it possible to do using just query without VBA?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    using pure sql, possibly but it would be very complicated. It is easy enough to find the previous record, but the problem you have is the rules for setting the grouping value.

    You also need to be careful with your terminology. Databases do not have any concept of 'previous' unless an order is specified. Your data is conveniently in ascending order of ct1 and ct2 - but what if the 2nd and 3rd records are swapped?

    I will assume 'previous' means when sorted by ct1 and ct2.

    For these situations I use a function which can be used in a query. For your situation, it might look like this

    Code:
    Function CTGroup(Optional t As Variant = "", Optional v As Variant = -1) As Variant
    Static X As Long
    Static n
    Static s
    
    
        If v = -1 Then 'reset group on criteria
        
            X = 0
            n = 0
            s = ""
            
        ElseIf t < s Then 'reset group on refresh (selecting all to copy/paste for example)
    
            X = 1
            s = t
            n = v
       
        'elseif something - if user clicks on individual records, group value can change for example user clicks on 3rd record then the 2nd record
    
        ElseIf t > s Or (t = s And v > n + 1) Then 'increment by one
        
            X = X + 1
            s = t
            n = v
            
        End If
      
        CTGroup = X
        
    End Function
    edit: note the 'elseif something' comment. You have not said what you are going to do with the data, whether it needs to be editable, etc so you may need additional code to account for that situation.



    Your query would be something like

    Code:
    SELECT Table1.ct1, Table1.ct2, ctgroup([ct1],[ct2]) AS ct3
    FROM Table1
    WHERE (((ctgroup())=False))
    ORDER BY Table1.ct1, Table1.ct2;
    which produces this result

    Click image for larger version. 

Name:	image_2024-01-03_122417094.png 
Views:	29 
Size:	8.4 KB 
ID:	51292

  3. #3
    vishv.kemonte is offline Novice
    Windows 10 Access 2021
    Join Date
    Jan 2024
    Posts
    6
    Please, could you send a file? Maybe I do smth wrong, but I have another result.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    what result are you getting?

    All you need to do is copy the function to a module and change the table name in your query to whatever your table is called.

    If your example data is not representative of what you actually have, that is your problem

    this is the table I created
    ct1 ct2
    KOH1 1
    KOH1 2
    KOH1 5
    KOH1 6
    KOH1 9
    KOH2 1
    KOH2 2
    KOH2 5
    KOH2 6
    KOH2 9

  5. #5
    vishv.kemonte is offline Novice
    Windows 10 Access 2021
    Join Date
    Jan 2024
    Posts
    6
    ct1 ct2 ct3
    KOH1 1 1
    KOH1 2 2
    KOH1 5 3
    KOH1 6 4
    KOH1 9 5
    KOH2 1 6
    KOH2 2 7
    KOH2 5 8
    KOH2 6 9
    KOH2 9 10

    I got this result.
    Yeah, the table is just like you showed.

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    looks like your ct2 field is text (it is showing as left justifed- should be numeric

  7. #7
    vishv.kemonte is offline Novice
    Windows 10 Access 2021
    Join Date
    Jan 2024
    Posts
    6
    Yeah, thanks a lot. Happy NY!!!!

  8. #8
    vishv.kemonte is offline Novice
    Windows 10 Access 2021
    Join Date
    Jan 2024
    Posts
    6
    Could you explain how you function works?
    For example, comment all lines. I would be very grateful to you.
    Your function is very useful for me and I would like to understand how it works.
    Thanks in advance.

  9. #9
    vishv.kemonte is offline Novice
    Windows 10 Access 2021
    Join Date
    Jan 2024
    Posts
    6
    I found out how the function worked. Thanks

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

Similar Threads

  1. Replies: 8
    Last Post: 02-24-2021, 10:35 AM
  2. Accessing, storing, and using data in query fields
    By faythe1215 in forum Programming
    Replies: 7
    Last Post: 04-08-2015, 12:42 AM
  3. Accessing field from previous form
    By howlettb in forum Access
    Replies: 6
    Last Post: 12-07-2012, 11:39 AM
  4. Replies: 6
    Last Post: 09-25-2009, 12:40 PM
  5. Accessing Code For a Query
    By TexMax007 in forum Programming
    Replies: 3
    Last Post: 08-29-2009, 08:08 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