Results 1 to 14 of 14
  1. #1
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32

    Question Use Switch for Field Parameter in Select Statement (MS Access)

    I've read here: http://stackoverflow.com/questions/5...ter-in-a-query that you can use SWITCH to define a field as a parameter in a query. The example solution is said to work in Access, but uses an UPDATE query. See below:



    Code:
    PARAMETERS WhichDate Long;
    UPDATE tblTarget AS t INNER JOIN tblSource AS s ON t.id = s.id
    SET t.date_assigned = Switch(WhichDate=1,date1,WhichDate=2,date2);
    How might the query look as a SELECT statement, or can this be done? I'm trying to use this to display 1 of 3 fields depending on which the user picks.
    However, the code does not work. Thoughts?

    Code:
    PARAMETERS WhichField Text ( 255 );
    SELECT p.myID, 
    s.docDate AS [myDate], 
    myField = Switch(WhichField="Sales1",'s.Sales1',WhichField="Sales2",'s.Sales2',WhichField="Sales3",'s.Sales3')
    FROM prod AS p INNER JOIN sumDaily AS s ON p.prodID = s.entityID
    GROUP BY p.myID, s.docDate;

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have never used a parameter in a query like this. Your Switch clause was wrong. The syntax is wrong for: "myField = Switch(Wh...)". You have to use the "AS" keyword.

    Maybe:
    Code:
    PARAMETERS WhichField Text ( 255 );
    SELECT p.MyID, s.docDate AS MyDate,
     Switch([WhichField]="Sales1","s.Sales1",[WhichField]="Sales2","s.Sales2",[WhichField]="Sales3","s.Sales3") AS myfield 
    FROM Prod AS p INNER JOIN sumDaily AS s ON p.MyID = s.entityID
    GROUP BY p.MyID, s.docDate;

  3. #3
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32
    Thanks Steve. That cleans things up, but when I type "Sales2" in the parameter, it's returning the text "s.Sales2" for each row, instead of using values from [s].[Sales2] for that field.

    I tried using sumDaily.Sales2, [sumDaily].[Sales2] and other combos, but it's still treating it as text. I wonder if this works with a SELECT statement at all?

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Doh!!

    Remove the quotes from s.Sales1, s.Sales2 & s.Sales3. These are fields, not text strings. You want the VALUE.

    Code:
    Switch([WhichField]="Sales1",s.Sales1,[WhichField]="Sales2",s.Sales2,[WhichField]="Sales3",s.Sales3) AS myfield

  5. #5
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32
    Code:
    PARAMETERS WhichField Text ( 255 );
    SELECT p.MyID, s.docDate AS MyDate,
     Switch([WhichField]="Sales1",s.Sales1,[WhichField]="Sales2",s.Sales2,[WhichField]="Sales3",s.Sales3) AS myfield 
    FROM Prod AS p INNER JOIN sumDaily AS s ON p.MyID = s.entityID
    GROUP BY p.MyID, s.docDate, myfield;
    This results in: Your query does not include the specified expression
    'Switch([WhichField]="Sales1",s.Sales1,[WhichField]="Sales2",s.Sales2,[WhichField]="Sales3",s.Sales3)' as a part of an aggregate function

    If I replace s.Sales1 with [sumDaily].[Sales1] and so on, then it asks for the value as if it's a parameter. Still no glory.

    Also to be clear, my desired output is: if parameter = "Sales1" to display the values in the field Sales1 from table sumDaily.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Can I ask you to tell readers what exactly you are trying to do?
    Is it to have a Parameter query, or is it to use the Switch()?

    If Parameters, then here's a sample

    Code:
    PARAMETERS [Bdate] DateTime, [Edate] DateTime;
    SELECT Orders.OrderID, Orders.OrderDate, Orders.ShipVia, Orders.ShipName
    FROM Orders
    WHERE (((Orders.OrderDate) Between [Bdate] And [EDate]));
    another
    Code:
    PARAMETERS TopStep IEEEDouble;
    SELECT xActionstbl.contractNum
    FROM xActionstbl
    WHERE step=TopStep;

  7. #7
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32
    Both.

    I want to use a parameter to tell the SQL which field to display in the query result.

    In my example, there are 3 options.

    Input:
    "Sales1" --> use/display Sales1 field from SumDaily table.
    "Sales2" --> use/display Sales2 field from SumDaily table.
    "Sales3" --> use/display Sales3 field from SumDaily table.

    This was demonstrated in my first post, but utilized an UPDATE query. My question is, can this be done in a SELECT statement? I can't get it to work. But I'm no SQL guru either. So I thought I would ask on this forum if anyone has experience doing this.

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    This code works for me on an existing query. I just added the SWITCH function.

    Code:
    PARAMETERS whichfield Long;
    SELECT Animal.AName, 
    Switch(whichfield=1,AnimalLocs.SightingDate,WhichField=2,AnimalCapture.CaptureDate,Whichfield=3
    ,AnimalLocs.GPSLat & " " & AnimalLocs.GPSLong) AS SwitchExample
    FROM (Animal INNER JOIN AnimalCapture ON Animal.AnimalId = AnimalCapture.AnimalId) INNER JOIN AnimalLocs ON Animal.AnimalId = AnimalLocs.AnimalId
    WHERE (((AnimalLocs.SightingDate)>[AnimalCapture].[CaptureDate]))
    ORDER BY Animal.AName, AnimalLocs.SightingDate;
    Last edited by orange; 03-12-2015 at 03:00 PM. Reason: spelling

  9. #9
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32
    Weird; I wonder if version of Access makes a difference? I'm using 2013 (I still get the aggregate function error point to Switch()).

  10. #10
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    My sample doesn't do any aggregation ---simple select with 3 tables.
    Last edited by orange; 03-12-2015 at 03:23 PM. Reason: spelling doesn't

  11. #11
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32
    That did it. It works. This can be handy.

    Thanks for your help!

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Can't use "MyField" in the Group By clause.
    I'm really not sure, but try:
    Code:
    PARAMETERS WhichField Text ( 255 );
    SELECT p.MyID, s.docDate AS MyDate,
     Switch([WhichField]="Sales1",s.Sales1,[WhichField]="Sales2",s.Sales2,[WhichField]="Sales3",s.Sales3) AS myfield 
    FROM Prod AS p INNER JOIN sumDaily AS s ON p.MyID = s.entityID
    GROUP BY p.MyID, s.docDate, Switch([WhichField]="Sales1",s.Sales1,[WhichField]="Sales2",s.Sales2,[WhichField]="Sales3",s.Sales3);

  13. #13
    bs0d is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    32
    I got it to run by changing GROUP BY to ORDER BY

  14. #14
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Great. I saw the post from Orange... I had created the query without the aggregation, but it was on your original query, so I left it as is. Glat you got it working.

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

Similar Threads

  1. Switch Statement
    By comfygringo in forum Queries
    Replies: 4
    Last Post: 09-04-2013, 03:07 PM
  2. Replies: 2
    Last Post: 04-04-2013, 03:13 PM
  3. switch statement in a access query
    By Dominic in forum Access
    Replies: 1
    Last Post: 03-27-2013, 11:32 PM
  4. Use Parameter in select statement (Sql in Access)
    By mohammadyou in forum Access
    Replies: 6
    Last Post: 06-04-2012, 01:40 PM
  5. use parameter in field name of select query
    By focosi in forum Queries
    Replies: 2
    Last Post: 02-11-2012, 10:34 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