Results 1 to 8 of 8
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Union Query Help

    I am trying to create a Union Query that will total the numbers from the months in 2010. I am trying to set it to display the month, the year, the type, and then the number input for that month, and then at the bottom display the "Grand Total" which adds all of the inputs. I have that set up with the following code:

    Code:
    Select Count(v.id) AS [Count#_], DatePart('yyyy',[date_entered]), AS Year_, DatePart('m',[date_entered]) AS Month_, DatePart('d',[date_entered]), AS Day_, [Visit_type] AS Visit_Type_
    From tbl_recordsinput AS v
    Group BY DatePart('yyyy',[date_entered]), DatePart('m',[date_entered]), DatePart('d',[date_entered]), [Visity_type]
    Union SELECT Count(v.id) AS [Count#_], "Grand Total" AS Year_, "" AS Month_, "" AS Day_, "" AS Visit_Type
    From tbl_recordsinput AS V
    Group BY "", "", "", "",;
    This code works just as it should. The catch I am running into is I am wanting it to prompt what visit type to display before the query is run, so I tried to add the line all in CAPS:

    Code:
    Select Count(v.id) AS [Count#_], DatePart('yyyy',[date_entered]), AS  Year_, DatePart('m',[date_entered]) AS Month_,  DatePart('d',[date_entered]), AS Day_, [Visit_type] AS Visit_Type_
    From tbl_recordsinput AS v
    Group BY DatePart('yyyy',[date_entered]), DatePart('m',[date_entered]), DatePart('d',[date_entered]), [Visity_type]
    HAVING (((TBL_RECORDSINPUT.VISIT_TYPE)=[ENTER VISIT TYPE]))
    Union SELECT Count(v.id) AS [Count#_], "Grand Total" AS Year_, "" AS Month_, "" AS Day_, "" AS Visit_Type
    From tbl_recordsinput AS V
    Group BY "", "", "", "",;
    This will prompt me for the visit type before the query runs, but when the query displays the results it displays all results not the specific visit type I was wanting it to ONLY display. Is there a way to modify this code, so that the query will only display the numbers input from the visit type that I am telling the query to display?

  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,642
    Untested, but try

    Select Count(v.id) AS [Count#_], DatePart('yyyy',[date_entered]), AS Year_, DatePart('m',[date_entered]) AS Month_, DatePart('d',[date_entered]), AS Day_, [Visit_type] AS Visit_Type_
    From tbl_recordsinput AS v
    WHERE (((v.VISIT_TYPE)=[ENTER VISIT TYPE]))
    Group BY DatePart('yyyy',[date_entered]), DatePart('m',[date_entered]), DatePart('d',[date_entered]), [Visity_type]
    Union SELECT Count(v.id) AS [Count#_], "Grand Total" AS Year_, "" AS Month_, "" AS Day_, "" AS Visit_Type
    From tbl_recordsinput AS V
    Group BY "", "", "", "",;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    That code prompts the user to input what they are wanting displayed, but the "Grand Total" is displaying all of the data input. Not just the data for what the user input to be displayed? I have googled this problem as well, but I can't seem to find out what is causing it. Any suggestions?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    I thought that was what you wanted, so I left it the way you had it. Each SELECT clause stands on its own, so you'd need the same WHERE clause on that section. As long as you use the exact same text, the user should only be prompted once.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    i don't think i follow wha your saying?

    but to answer your question i am only wanting the total count of the visit type tthat the user inputs.

    i changed thhe code to what you gave me and it works by only displaying the visitt type input. is it possible to tweak it again to only display the "grand total" of the visit type selected?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Select Count(v.id) AS [Count#_], DatePart('yyyy',[date_entered]), AS Year_, DatePart('m',[date_entered]) AS Month_, DatePart('d',[date_entered]), AS Day_, [Visit_type] AS Visit_Type_
    From tbl_recordsinput AS v
    WHERE (((v.VISIT_TYPE)=[ENTER VISIT TYPE]))
    Group BY DatePart('yyyy',[date_entered]), DatePart('m',[date_entered]), DatePart('d',[date_entered]), [Visity_type]
    Union SELECT Count(v.id) AS [Count#_], "Grand Total" AS Year_, "" AS Month_, "" AS Day_, "" AS Visit_Type
    From tbl_recordsinput AS V
    WHERE (((v.VISIT_TYPE)=[ENTER VISIT TYPE]))
    Group BY "", "", "", "",;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    That worked perfectly! Thanks for the help!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    No problemo!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Please help me out with union query
    By radicrains in forum Access
    Replies: 1
    Last Post: 10-29-2010, 01:48 AM
  2. Union query only returning 255 characters
    By jpkeller55 in forum Queries
    Replies: 25
    Last Post: 10-05-2010, 05:51 AM
  3. Union Query Question
    By jpkeller55 in forum Access
    Replies: 2
    Last Post: 09-24-2010, 10:18 AM
  4. Union Query (choosing between two fields)
    By naveehgreen in forum Programming
    Replies: 1
    Last Post: 06-17-2010, 03:24 PM
  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