Results 1 to 12 of 12
  1. #1
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067

    Need to add a value to a query

    I have the following query

    Code:
    SELECT dbo_tbl_HPCODEs.PRODUCTLINE
    FROM dbo_tbl_HPCODEs
    GROUP BY dbo_tbl_HPCODEs.PRODUCTLINE;
    For the purpose of one query I need to add 1 specific value to the results.

    My first thought was
    Code:
    SELECT dbo_tbl_HPCODEs.PRODUCTLINE
    FROM dbo_tbl_HPCODEs
    GROUP BY dbo_tbl_HPCODEs.PRODUCTLINE
    Union All
    Select "ACO" ;
    but I realized that won't work because it's not from a table it's just 1 specific value. The Results from the first query is

    COMMERCIAL
    COMM-POS
    SENIOR
    MEDI-CAL


    MEDI-CAL EXPANSION
    MEDICONNECT
    COVERED CA

    This is for a combo box and I need to Add "ACO" to that list. Any idea how I can do that. Tried VBA using the AddItem for the combo box but that only works if it's a value list. I need it to be the query.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    you dont use: AddItem
    all items should come from a query or table.
    if tProductLine is the table then add ACO to the table.

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

    SELECT dbo_tbl_HPCODEs.PRODUCTLINE
    FROM dbo_tbl_HPCODEs
    GROUP BY dbo_tbl_HPCODEs.PRODUCTLINE
    UNION
    SELECT "ACO"
    FROM dbo_tbl_HPCODEs
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    it's not from a table it's just 1 specific value
    So you fake it.
    Code:
    SELECT dbo_tbl_HPCODEs.PRODUCTLINE, 
    FROM dbo_tbl_HPCODEs
    UNION
    SELECT "ACO" AS Whatever FROM 
    dbo_tbl_HPCODEs;
    It shouldn't matter what you put as the alias (AS part) because it's not going to show anyway, but ACO will be added to the values returned from the table. When you think about it, adding an alias field to a query based on a table is the same thing - it doesn't exist in that table either. Same goes for adding a specific value - it doesn't exist in the table. I left off the Group By since it's not a Totals query and I don't see why it's needed for a combo box. If you're getting multiple results, you need either the DISTINCT or DISTINCT ROW predicate.

    Paul, you beat me again. One second you're not there, the next...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  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
    Fastest fingers in the west!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067
    thank you that worked.

  7. #7
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I can only guess as to which you are referring.

  8. #8
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067
    Sorry should have been more specific. I added the from so my query now looks like this

    Code:
    SELECT dbo_tbl_HPCODEs.PRODUCTLINE
    FROM dbo_tbl_HPCODEs
    GROUP BY dbo_tbl_HPCODEs.PRODUCTLINE
    UNION ALL select distinct "ACO" as PL
    from dbo_tbl_hpcodes;
    Works perfectly thanks

  9. #9
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Good that it works, but FYI, UNION returns unique values. UNION ALL returns duplicates if there are any. Thus UNION ALL and SELECT DISTINCT are contradictory. Apparently, the latter over-rides the former, or in your case there are no duplicates anyway.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You and I both dropped the ALL to get rid of the duplicates that the second SELECT would have returned. I'm guessing what Ray posted still works. The ALL allows duplicates over the whole result set, but the DISTINCT would prevent the second SELECT from returning duplicates. Might even be more efficient, since UNION ALL is more efficient than UNION.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067
    never thought of using just Union. Tried it and it works either way. I'll keep what I originally posted because like you I think that the Union All is more efficient but I do get the same result either way.

  12. #12
    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 point was you would not get the same result dropping the DISTINCT:

    SELECT dbo_tbl_HPCODEs.PRODUCTLINE
    FROM dbo_tbl_HPCODEs
    GROUP BY dbo_tbl_HPCODEs.PRODUCTLINE
    UNION ALL
    select "ACO" as PL
    from dbo_tbl_hpcodes;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

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