Results 1 to 6 of 6
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    help with Union Query

    I have three tables with some fields that are similar but each table has a few unique fields. I want to select the records which match different criteria for each table. So I added a WHERE line to each table. This query returns the correct results.



    SELECT ID, Field1 as [SN], BlueID, HWr, FW
    FROM TABLE1
    WHERE Field1 <> "UN"
    UNION ALL
    SELECT ID, Field1, BlueID, HWr, FW
    FROM TABLE2
    WHERE Field1 <> "NA"
    UNION ALL
    SELECT ID, Field1, BlueID, HWr, FW
    FROM TABLE3
    WHERE Field1 is not null
    ORDER BY [SN]

    Now that I have a combined view off all the records in one query, I created a 3 Combo Boxes on a Form which each displays Field1 from each Table. I have an Option Box which only displays the Combo Box based on the option choice. I set the combobox.visible = false on the other 2 combo boxes.

    First I created a Public variable called SNS which gets populated by the results of the Combo Box on the form that I just mentioned.
    I also have a Public Function called SNSearch() which populates also by the results of the Combo Box on the form that I just mentioned.

    I tried adding both above options to my Union query as follows
    WHERE [SN] = SNS; or
    WHERE [SN] = SNSearch();

    but both give me an error.

    As a workaround, I created a second select query based on my union query and simply put SNSearch() in the criteria of the SN field. But is there a way to modify the union query without creating a second query.

    Thanks
    Sam

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You can't put variables in an Query.
    you can use form objects as variables...
    select * from table where [field]=forms!myForm!txtBox
    or
    You CAN assemble it in code THEN run it.....
    Code:
    sSql = "select * from table where [field]='" & sSns & "'"
    docmd.runSql sSql

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  4. #4
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    I don't know. That's what I found in the help

  5. #5
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Thanks.

    My workaround with a second query works so I think I will just keep that.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Union ALL will have duplicates if they exist in any of the components of your UNION.
    UNION (without ALL) removes any duplicates.

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

Similar Threads

  1. Replies: 4
    Last Post: 12-20-2015, 02:35 PM
  2. Union Query
    By guptaa13 in forum Access
    Replies: 4
    Last Post: 09-24-2014, 07:04 PM
  3. Replies: 8
    Last Post: 10-22-2012, 07:43 PM
  4. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 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