Results 1 to 3 of 3
  1. #1
    pmplyons is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    1

    Date function within a Union Query

    I'm building an income statement report in MS Access 2007 and I'm attempting to avoid having to build out sub-reports for each expense/revenue type. I've written a union query in SQL and I'm wondering if I can put in a dim function to avoid having to repopulate my date parameters? For example, I have the following code already working but have to put the begin date for the current year and prior year as well as the end of the measurement period for both dates a number of times. I was hoping I could write a dim function that would require me to only enter the date parameters one time each.

    Select Sum(GL_Activity.Amount) AS PTOR


    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Begin Current Year Date] And [End of Current Period]))
    UNION SELECT Sum(GL_Activity.Amount) AS PTOR
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Prior Year Begin Date] And [End of Prior Year Period]))
    Union SELECT Sum(GL_Activity.Amount) AS ForeignTax
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Begin Year Date] And [End of Period]) AND ((SAP_GLCodes.GAAP_PTOR)="ForeignTax"))
    Union SELECT Sum(GL_Activity.Amount) AS ForeignTax
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Prior Year Begin Date] And [End of Prior Year Period]) AND ((SAP_GLCodes.GAAP_PTOR)="ForeignTax"))
    Union SELECT Sum(GL_Activity.Amount) AS FIT
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Begin Year Date] And [End of Period]) AND ((SAP_GLCodes.GAAP_PTOR)="FIT"))
    Union SELECT Sum(GL_Activity.Amount) AS FIT
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Prior Year Begin Date] And [End of Prior Year Period]) AND ((SAP_GLCodes.GAAP_PTOR)="FIT"))
    Union SELECT Sum(GL_Activity.Amount) AS SIT
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Begin Year Date] And [End of Period]) AND ((SAP_GLCodes.GAAP_PTOR)="SIT"))
    Union SELECT Sum(GL_Activity.Amount) AS SIT
    FROM GL_Activity INNER JOIN SAP_GLCodes ON GL_Activity.Account = SAP_GLCodes.Account
    WHERE (((GL_Activity.Period) Between [Prior Year Begin Date] And [End of Prior Year Period]) AND ((SAP_GLCodes.GAAP_PTOR)="SIT"))

    Also, is there a way to return my results horizontally in my union query with a different column name? It would be easier to pull from my report.

    Thanks

  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,640
    A Dim would only be appropriate in code. For a query, the most common solution would be to have the user enter the dates on a form, and have the query criteria point there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Let me add to what pbaldy said. As a guess here if you put in a date you can figure the beginning of the year, and the previous year dates just based on doing one point of data entry so it would not be burdensome for you to create a form and have the other search field values be populated. Then you would reference them in your union query with

    [forms]![formname]![searchfieldname]

    Where you'd, of course, substitute your form and field names where I have [formname] and [searchfieldname]

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

Similar Threads

  1. Date function to query records and Sum records
    By sullynivek in forum Queries
    Replies: 0
    Last Post: 04-05-2011, 08:37 AM
  2. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 PM
  3. Union Query Help
    By jo15765 in forum Queries
    Replies: 7
    Last Post: 01-06-2011, 05:46 PM
  4. Please help me out with union query
    By radicrains in forum Access
    Replies: 1
    Last Post: 10-29-2010, 01:48 AM
  5. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 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