Results 1 to 14 of 14
  1. #1
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44

    SQL Query using IF Statement???

    I'm not sure if ive taken the right path, the code below pulls 4 stock location tables together and asks the user firstly for an aisle which is 2 left characters (Expr1) and then the level which is the Right end character (Expr2) e.g A, B, C, D etc
    This is my question, On Level 'A' I have 'G' as well and on Level 'B' I have 'H' as well, now I could run 2 separate reports one for A and one for G and the same for B and H.
    Is there a way that when someone requests Level A it automatically includes G and also if someone requests Level B it automatically includes H ????

    Code:
    SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Opus.*FROM Opus
    WHERE (((Left([Location],2))=[Enter Aisle]) AND ((Right([Location],1))=[Enter Level]))
    
    
    UNION SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Wrigley_Res.*
    FROM Wrigley_Res
    WHERE (((Left([Location],2))=[Enter Aisle]) AND ((Right([Location],1))=[Enter Level]))
    
    
    UNION SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Glenmark_SAP.*
    FROM Glenmark_SAP
    WHERE (((Left([Location],2))=[Enter Aisle]) AND ((Right([Location],1))=[Enter Level]))
    
    
    UNION SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Wrigley_Act.*
    FROM Wrigley_Act
    WHERE (((Left([Location],2))=[Enter Aisle]) AND ((Right([Location],1))=[Enter Level]));
    Hope this makes sense

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why do you have 4 tables? Why not 1 table with another field for site (Wrigley_Res, Glenmark_SAP, etc)?

    Don't do filtering in UNION. Treat the UNION as the normal table it should be. Build another query that references the UNION.

    So you want to do this compound filtering only for these two pairs? Not for C, D, etc?

    I think better build VBA code to dynamically construct filter criteria. Review: http://allenbrowne.com/ser-62.html
    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.

  3. #3
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44
    Sadly It has to be 4 tables because each table is linked to an Excel sheet which is refreshed via ODBC from 4 different systems Opus, PKMS, SAP etc :-(
    I am new to this which is why I felt I may have taken the wrong path, the example works great but just wanted 1 report instead of 2, it's only A&G and B&H..... c, d, d, e etc are all ok

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Keep your 4 linked Excel files but import from those to one table.
    Just because the data originates from different sources, it doesn't mean it needs to be stored separately.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Can be 1 report. The VBA constructs filter criteria and then passes the criteria to report in the OpenReport method.


    I thought a conditional in query would be rather complex, hence the recommendation for VBA, but the following might accomplish:

    WHERE Expr1 = [Enter Aisle] AND (Expr2 = [Enter Level] OR Expr2 = Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]))


    I never use dynamic parameterized query, especially not with input popup - can't validate user input. Have the parameter reference a control on form for input.
    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.

  6. #6
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44
    Ok my report didn't like that it seemed to have listed everything
    I'm going to look into merging all 4 tables into 1 table as mentioned above then use VBA as I think the SQL could be a bit tricky for me :-)

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    You may have read my previous post before I did an edit on the criteria expression. Review again.

    Don't entirely agree with the merged table idea since these worksheets are updated periodically. Maintaining that table can be a complicated process of its own.

    A merged table won't resolve the report issue. Get your UNION and the report working first. Look at the merging later if you think that is worthwhile.
    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.

  8. #8
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44
    Yeah definitely confused now, are you saying replace the WHERE line like below, I'm getting a 'Syntax Error'

    Code:
    SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Opus.*
    FROM Opus
    WHERE Expr1 = [Enter Aisle] AND (Expr2 = [Enter Level] OR Expr2 = Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]))
    
    
    UNION SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Wrigley_Res.*
    FROM Wrigley_Res
    WHERE Expr1 = [Enter Aisle] AND (Expr2 = [Enter Level] OR Expr2 = Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]))
    
    
    UNION SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Glenmark_SAP.*
    FROM Glenmark_SAP
    WHERE Expr1 = [Enter Aisle] AND (Expr2 = [Enter Level] OR Expr2 = Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]))
    
    
    UNION SELECT Left([Location],2) AS Expr1, Right([Location],1) AS Expr2, Wrigley_Act.*
    FROM Wrigley_Act
    WHERE Expr1 = [Enter Aisle] AND (Expr2 = [Enter Level] OR Expr2 = Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level])));

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I said in post #2 not to do filtering in the UNION, build another query that references the UNION query. Apply filter in the second query.


    If you do this filter in the UNION will have to repeat the Left() and Right() calcs in the WHERE clause everywhere the Expr1 and Expr2 alias names are used.


    Shouldn't you have gotten syntax error with the first version?
    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.

  10. #10
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44
    Ok, I may have bitten off more than I can chew.
    Tomorrow I will look at the terminology UNION as well as creating a new query as I'm totally confused.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    UNION will not return duplicate records. Use UNION ALL if you want every record even if duplicates. You are using wildcard to return fields. This assumes fields are the same in all tables and in the same order in table design.

    The first SELECT defines field names and alias field names do not have to be repeated. Calculate a field to identify the source table. Suggest you use more meaningful names than Expr1 and Expr2. Consider:

    SELECT Left([Location],2) AS Aisle, Right([Location],1) AS Lvl, "O" AS Source, Opus.* FROM Opus
    UNION SELECT Left([Location],2), Right([Location],1), "WR", Wrigley_Res.* FROM Wrigley_Res
    UNION SELECT Left([Location],2), Right([Location],1), "GS", Glenmark_SAP.* FROM Glenmark_SAP
    UNION SELECT Left([Location],2), Right([Location],1), "WA", Wrigley_Act.* FROM Wrigley_Act;

    Save that query and treat it like a table (except cannot edit data via UNION) and build another query that references the saved UNION query name and apply suggested filter criteria. I tested the filter syntax and it works for me.
    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.

  12. #12
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44
    I'm almost there, I have setup the query as you mentioned above and called it QueryTableMerge but i used UNION ALL as I needed duplicates. Ive setup another query to select the Aisle/Level records. All works ok but Ive tried adding your previous WHERE statement so that when 'A' is selected it includes 'G' but I only get 'G' and if I enter 'B' I only get 'H' ??

    Code:
    SELECT qryTableMerge.Aisle, qryTableMerge.Lvl, qryTableMerge.Location, qryTableMerge.Product, qryTableMerge.Description, qryTableMerge.Reference, qryTableMerge.Quantity, qryTableMerge.Customer, qryTableMerge.Date, qryTableMerge.Batch, qryTableMerge.SourceFROM qryTableMerge
    WHERE ((qryTableMerge.Aisle)=[Enter Aisle]) AND ((qryTableMerge.Lvl)=Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]));
    Any thoughts???

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    You did not exactly replicate the suggested WHERE:

    WHERE Expr1 = [Enter Aisle] AND (Expr2 = [Enter Level] OR Expr2 = Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]))

    so

    WHERE qryTableMerge.Aisle=[Enter Aisle] AND (qryTableMerge.Lvl=[Enter Level] OR qryTableMerge.Lvl=Switch([Enter Level]="A", "G", [Enter Level]="B", "H", True, [Enter Level]));
    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.

  14. #14
    bishmedia is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    44
    Thankyou June7, it's been quite a learning curve for me this week but with your help this works really great and to boot with what Ive learnt this week I can use it elsewhere.

    Once again, thankyou.

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

Similar Threads

  1. More Help with SQL Statement for a Query
    By Antonio in forum Queries
    Replies: 6
    Last Post: 05-09-2018, 05:42 AM
  2. Replies: 8
    Last Post: 05-08-2018, 07:27 AM
  3. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  4. Replies: 3
    Last Post: 07-10-2012, 05:23 AM
  5. query iif statement help
    By swat in forum Queries
    Replies: 4
    Last Post: 09-30-2011, 11:48 AM

Tags for this Thread

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