Results 1 to 6 of 6
  1. #1
    AlexBen is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Posts
    10

    dsum

    Good day! am a newbie with Access so please bear with me... I tried using Dsum but am just getting a blank result... here is the SQL



    SELECT tblMain.SowID, tblpiglet.SowIds, tblMain.BornAlive, Abs([WeanNumber]) AS Wean, tblMain.SplitNumber, tblpiglet.PigNum, tblpiglet.Reason, IIf(IsNull([Reason]),[PigNum],[PigNum]*-1) AS Foster, DSum("Foster","tblpiglet","SowIds = '" & [tblMain]![SowID] & "'") AS NetFoster FROM tblMain INNER JOIN tblpiglet ON tblMain.SowID = tblpiglet.SowIds;




    Thanks in advance!

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Could be one or a few issues there.
    - not "Foster" but Foster. [Foster] would be another possibility but I think that is somewhat remote.
    - in Access, DSum is a Domain (table or query) function. Since Foster is not in the domain but is an alias for your IIF function, I doubt you can Dsum on it even as Foster or [Foster]. Perhaps you can Sum it instead. However, as Sum I think you'll have to group by at least one field. You might have to use a separate query or function to perform the sum.

    Sometimes it's a great help (even necessary) if you copy the db, compact/repair the copy, zip it and upload to a post.
    If info is sensitive, perhaps see Randomizing Data for Posted db's
    Last edited by Micron; 09-27-2021 at 05:39 PM. Reason: added comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,944
    I would expect anything with ID in it to be numeric, so no need for single quotes, they are for strings?
    Code:
    "SowIds = '" & [tblMain]![SowID] & "'")
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    AlexBen is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Posts
    10
    Thanks Micron and Welshgasman for your reply. I ended up making two queries to get the sum.

  5. #5
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    In normal T-SQL you can only use aliases in the ORDER BY clause, because that is executed after the SELECT. Normally a query is parsed and executed in the following order:
    first the from,
    then the where,
    then the GROUP BY,
    then the HAVING
    then the SELECT part, here the aliases are created
    and as last the ORDER BY, so here the aliases already exist

    As each part is executed as a whole, the aliases only come to exist after the complete select part is . So it cannot be used in the select itself.

    Of course, this is T-SQL and I know Access SQL is different from T-SQL. But maybe it works the same way for this bit.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The IIf expression could be within the DSum(), however, a subquery should be more efficient.
    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.

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

Similar Threads

  1. Dsum
    By Ihthisham in forum Queries
    Replies: 9
    Last Post: 07-12-2021, 11:02 AM
  2. Dsum help
    By messier in forum Queries
    Replies: 2
    Last Post: 05-15-2020, 12:30 PM
  3. Dsum
    By doddiah in forum Access
    Replies: 5
    Last Post: 07-20-2015, 02:56 PM
  4. DSum
    By balajigade in forum Queries
    Replies: 1
    Last Post: 04-26-2015, 10:26 AM
  5. How do I use the DSum
    By Ironclaw in forum Access
    Replies: 1
    Last Post: 08-25-2010, 07:35 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