Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44

    Conditional progressive sum

    Hi all.
    I have a query summing up all the work periods of some marine officers. They're supposed to do at max 6 periods onboard. This is the query structure, where "TOT" is a calculated field which gives the difference between Al (to) and Da (from).

    Click image for larger version. 

Name:	06.jpg 
Views:	17 
Size:	96.3 KB 
ID:	33458



    This is how it looks like if I select a record (1664) related to an officer:

    Click image for larger version. 

Name:	07.jpg 
Views:	17 
Size:	62.8 KB 
ID:	33459

    I would need to set another column calculating the "progressive" days sum, taking it from the totals. Some like that (made in Excel as example):

    Click image for larger version. 

Name:	08.jpg 
Views:	17 
Size:	61.7 KB 
ID:	33460

    In other words I would need a formula asking the query to calculate the progressive sum of TOT for the 1 to 6 onboard periods for each officer ID.

    Can anyone help me to build the right formula?

    (if not in the same query, I could also put the formula on a dedicated field in the form recalling the query).

    Thank you in advance!

    Davide

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi Davide,
    You need a cumulative sum or a running totals query - https://support.microsoft.com/en-us/...crosoft-access
    In your case would look something like this:
    Prog: DSum("[TOT_gg]","[anag_tot_imbarchi]","[ID_anag_allevi] ='" & [USER] & "' AND [Imbarco_n] <= " & [Imbarco_n])

    Cheers,
    Vlad

  3. #3
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Quote Originally Posted by Gicu View Post
    Hi Davide,
    You need a cumulative sum or a running totals query - https://support.microsoft.com/en-us/...crosoft-access
    In your case would look something like this:
    Prog: DSum("[TOT_gg]","[anag_tot_imbarchi]","[ID_anag_allevi] ='" & [USER] & "' AND [Imbarco_n] <= " & [Imbarco_n])

    Cheers,
    Vlad
    Thenk you Vlad.
    It says there is an error in syntax, identifying this in bold: DSum("[TOT_gg]","[anag_tot_imbarchi]","[ID_anag_allevi] ='" & [USER] & "' AND [Imbarco_n] <= " & [Imbarco_n])

    And then... what do you mean with "USER"?

    Thanks.
    D.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Davide,

    Please check the spelling, it was just meant as an example as I am not sure of your field names. [USER] should be an alias of your [ID_anag_allevi...]field. I would create a new query (anag_tot_imbarchi_prog) based on your original one, bring the fields you need from the source query and add an alias for user id (USER:ID_anag_allevi) and one for the IMB: imbarchi_n and us those in the formula:

    DSum("[TOT_gg]","[anag_tot_imbarchi]","[ID_anag_allevi] =" & [USER] & " AND [Imbarco_n] <= " & [IMB])

    Have a look at the link from Microsoft, it should help you understand how this works.

    Cheers,
    Vlad

  5. #5
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Quote Originally Posted by Gicu View Post
    Davide,

    Please check the spelling, it was just meant as an example as I am not sure of your field names. [USER] should be an alias of your [ID_anag_allevi...]field. I would create a new query (anag_tot_imbarchi_prog) based on your original one, bring the fields you need from the source query and add an alias for user id (USER:ID_anag_allevi) and one for the IMB: imbarchi_n and us those in the formula:

    DSum("[TOT_gg]","[anag_tot_imbarchi]","[ID_anag_allevi] =" & [USER] & " AND [Imbarco_n] <= " & [IMB])

    Have a look at the link from Microsoft, it should help you understand how this works.

    Cheers,
    Vlad
    Thanks Vlad. But it says there is a syntax mistake... but I can't got why or where...

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Can you show what you have? Screen- shot or even better a small Access db with the table (empty or a few sample records) and the query.

    Cheers,
    Vlad

  7. #7
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    I wouldn't know how to attach an entire DB. Anyway the situation is like I described in the first message. I just changed the fields name. Now the starting query is this one:

    Click image for larger version. 

Name:	09.jpg 
Views:	11 
Size:	81.7 KB 
ID:	33546

    Behind is like that:

    Click image for larger version. 

Name:	10.jpg 
Views:	11 
Size:	106.1 KB 
ID:	33547

    Where "ID_imbarchi_anagrafica" is linked to the ID main form.
    "N" is the progressive number of onboard experiences.
    "Dal" is from (date)
    "Al" is to (date)
    "TOT" is the field calculating the number of days spent on board.
    I tried to insert the "DSum" code in the sixth colum to have the progressive sum, but it says there is a mistake in the syntax.
    This is the code adapted to the new field names:

    Code:
    TOTALE: DSum("[TOT]","[imbarchi_stageqry]","[ID_imbarchi_anagrafica] =" & [ID_imbarchi_anagrafica]" & " AND [N] <= " & [N])
    This is the screen message:

    Click image for larger version. 

Name:	11.jpg 
Views:	11 
Size:	130.7 KB 
ID:	33548

    It says: "Syntax error in the expression - Operating or operator missing, letter or comma invalid or input text not included in quotation marks".

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    The second argument in the dsum is the query name which doesn't seem right. Also, you need to use aliases in the condition part. Assuming the imbarchi_stageqry query you use in the dsum does also have a field called TOT and has the ID_imbarchi_anagrafica and N fields you need to rename the fields in the new query (imbarchi_sintesistage) to be different:
    IDImbarchi:ID_imbarchi_anagrafica And NUM:N

    TOTALE: DSum("[TOT]","[imbarchi_stageqry]","[ID_imbarchi_anagrafica] =" & [IDImbarchi]" & " AND [N] <= " & [NUM])

    Cheers,
    Vlad

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Davide,

    Have a look at the attached file.

    Cheers,
    Vlad
    Attached Files Attached Files

  10. #10
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Quote Originally Posted by Gicu View Post
    The second argument in the dsum is the query name which doesn't seem right. Also, you need to use aliases in the condition part. Assuming the imbarchi_stageqry query you use in the dsum does also have a field called TOT and has the ID_imbarchi_anagrafica and N fields you need to rename the fields in the new query (imbarchi_sintesistage) to be different:
    IDImbarchi:ID_imbarchi_anagrafica And NUM:N

    TOTALE: DSum("[TOT]","[imbarchi_stageqry]","[ID_imbarchi_anagrafica] =" & [IDImbarchi]" & " AND [N] <= " & [NUM])

    Cheers,
    Vlad
    Argh... getting crazy... I didn't know about the ALIAS trick. Anyway there's still something wrong. The result is #error:

    STRUCTURE

    Click image for larger version. 

Name:	004.jpg 
Views:	11 
Size:	115.1 KB 
ID:	33550

    TABLE

    Click image for larger version. 

Name:	005.jpg 
Views:	11 
Size:	93.4 KB 
ID:	33551

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Can you try and copy it from the file I've sent you and see if you still get the error?

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Looking at your data I see you have N as text not number so you need to wrap it in single quotes:

    TOTALE: DSum("[TOT]","[imbarchi_stageqry]","[ID_imbarchi_anagrafica] =" & [IDImbarchi]" & " AND [N] <= '" & [NUM] & "'")

  13. #13
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Just realized that you might have problems if you keep N as text when you compare text. If you cannot change it in the table do it in the imbarchi_stageqry by using a N_Int:CInt([N]) and change the field names accordingly in the final query/

    Vlad

  14. #14
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Hi all.
    Vlad's code works well. The core problem was just the field N as text. I changed it to number and now everything works in the queries.
    But not in the form. Take a look, this is the form where the progressive sum is shown (fake data as example), with ID = 115:

    Click image for larger version. 

Name:	006.jpg 
Views:	7 
Size:	122.3 KB 
ID:	33580

    But if I browse to the next record (ID = 116) to see the data of the next seafarer and it has no embarkation data, it pops up an error, like:

    Click image for larger version. 

Name:	007.jpg 
Views:	8 
Size:	142.7 KB 
ID:	33582

    "Syntax error (missing operator) in the query expression "ID_imbarchi_anagrafica] = 116 etc...

    Maybe I should put some Nz in the code?

    Thanks!

  15. #15
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Davide,

    It depends on your needs, you could use NZ or you could put Not Null in the Dal and Al fields of the query to force the calculation to occur only for the right records.

    Cheers,
    Vlad

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Progressive number generator
    By Beppe in forum Programming
    Replies: 2
    Last Post: 09-19-2017, 07:11 AM
  2. Conditional Query with conditional results
    By Jeff_in_KCMO in forum Queries
    Replies: 4
    Last Post: 09-10-2014, 01:44 PM
  3. Conditional formatting
    By keiath in forum Forms
    Replies: 28
    Last Post: 01-27-2014, 08:36 AM
  4. Replies: 6
    Last Post: 05-26-2013, 09:10 PM
  5. Conditional Printing
    By anoob in forum Forms
    Replies: 28
    Last Post: 03-18-2011, 01:51 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