Results 1 to 7 of 7
  1. #1
    ryanjames17 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Location
    Houston TX
    Posts
    5

    Running Total / Count In A Query (dsum / Dcount), Access 2010

    Hi there, I have a question about setting up a running total or count in an access query. I know this isn't the most efficient way to run things, but I trying it anyways, because it could automate a manual process for me.



    A little background, I have a Monthly Production table (this is the base dataset, see 'Monthly Production Table Fields' image which is attached) which stores monthly production data for a number of wells. I have built a series of queries which pick the peak production rate in the first X months (user defined with a prompt) for each well and then normalizes that data, or rather it takes the monthly production from that month forward and reports it for all of the wells in the Monthly production table. The end result or query ( PBOE_XMo_Norm_Prod ) is shown in the attached image 'Query Results'. The problem is that the producing month starts at whatever is the peak month for each well, in the attached image it is month 4.

    What I want to do is ad a running count that starts at 1 and counts each month consecutively but then resets to 1 when the well or category changes. I've done a bunch of research, but I can't seem to get the right results. I tried using a DSum initially as that was what I could find the most examples of. Regardless, what I seem to constantly get is a count or a sum of all the records in that field (in the example: Liquids) in the original Monthly Production Table. Any tips on what to do? Is it because I am prompting for data in the first query? Please advise, I've attached screenshots of some of the DB as well as the SQL text. Any advice or suggestion is appreciated.

    Thanks,

    SQL:

    SELECT PBOE_XMo_Norm_Prod.Entity AS EntityID, PBOE_XMo_Norm_Prod.Prod_Mo AS NormMo, PBOE_XMo_Norm_Prod.Liquid, DCount("[Monthly Production].[Liquid]","[monthly production]","[monthly production].[Producing Month]=" & [NormMo] And "[monthly production].[entity]='" & [EntityID] & "'") AS Expr1
    FROM PBOE_XMo_Norm_Prod;

    Click image for larger version. 

Name:	Monthly Production Table Fields.PNG 
Views:	27 
Size:	30.6 KB 
ID:	18606Click image for larger version. 

Name:	Query Design View.PNG 
Views:	26 
Size:	35.6 KB 
ID:	18607Click image for larger version. 

Name:	Query Results.PNG 
Views:	29 
Size:	22.7 KB 
ID:	18608

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Your concatenation is off. Try

    DCount("[Liquid]","[monthly production]","[Producing Month]=" & [NormMo] & " And [entity]='" & [EntityID] & "'")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    Your runng Sum (Dsum) must have a WHERE clause to differentiate each record (and before it). (else youll get the same # for every record)

    Dsum([fld],[tbl], where)
    the 1st rec is the 1st rec
    for the 2nd rec MUST be = 2nd rec and all the ones before it. , so where = "[entity] = [entid] and [normMo]<= [normMo] )

  4. #4
    ryanjames17 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Location
    Houston TX
    Posts
    5
    Thanks Paul, when I tried that, all I got was the answer 1. The concantentation is set up the way it is I think, because one data type is text and the other is numeric. But maybe I did that wrong.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The delimiters are because one field is text, but your concatenation was off. That said, I missed that you need a <. Try


    DCount("[Liquid]","[monthly production]","[Producing Month]<=" & [NormMo] & " And [entity]='" & [EntityID] & "'")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    ryanjames17 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Location
    Houston TX
    Posts
    5
    ranman256,

    Thanks but where to I stick the WHERE clause? Is it part of the criteria in the Dsum formula? I tried doing this: Expr1: DCount("[Monthly Production].[Liquid]","[Monthly Production]","'Where [Entity]='" & "'[EntityID]'" And "[NormMo]<=" & [Prod_Mo]) with the single apostrophe in front of and behind the WHERE but it didn't change the result. Also should I be referring to the Monthly Production Table or the Query I'm running this on?

  7. #7
    ryanjames17 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Location
    Houston TX
    Posts
    5
    That worked! Thankyou... well sort of work. The query is running properly, but it is starting at 4 or the peak production month, which matches the NormMo in this query. I think this is because it's running it on the original data table and not the Query that I am running this query on. Any idea how to get it to work on this query?

    Thanks for your help though, this is a breakthrough.

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

Similar Threads

  1. Replies: 2
    Last Post: 04-15-2014, 01:59 PM
  2. Replies: 34
    Last Post: 03-03-2014, 09:24 AM
  3. running or sub total using sql query in access
    By learning_graccess in forum Access
    Replies: 4
    Last Post: 10-15-2011, 05:40 AM
  4. DSum or DCount
    By vdanelia in forum Forms
    Replies: 1
    Last Post: 03-01-2011, 03:59 AM
  5. Running update query in access 2010
    By dbansal in forum Queries
    Replies: 1
    Last Post: 09-03-2010, 10:57 AM

Tags for this Thread

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