+ Reply to Thread
Results 1 to 10 of 10

access query design help - adding field while keeping all records

  1. #1
    Registered User
    Join Date
    02-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    17

    access query design help - adding field while keeping all records

    I have 2 tables with a common field (Table A and Table B). There is another field on Table B with 'yes' as the cell data in each record. I want to query the tables to match the common field on Table B to Table A with the end result being Table A having a new field with 'yes' as the record data where the two common fields matched, and having a blank or null entry where there was no match.

    If this were excel I'd just run a vlookup. I'm trying to do something similar in access.

    Thx for any help.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: access query design help - adding field while keeping all records

    You cannot modify the structure of a table with a query (Generalisation ignoring DDL), but you can write a query to create a new table with the structure you want.

    MakeTable query: SELECT a.*, b.ExtraField INTO NewTable FROM a INNER JOIN b ON a.ID = b.ID;

    If you need the original Table A modified and then updated, you can use VBA to modify the structure before running the query.
    Last edited by cytop; 05-11-2016 at 08:17 AM.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: access query design help - adding field while keeping all records

    How about: (assuming all matches have Yes in tableB)

    Select Table1.*, [Table2].[YesFieldName] FROM [Table1] LEFT JOIN [Table2] ON [Table1].[CommonFieldName] = [Table2].[CommonFieldName];

    I am assuming of course that you want a query that returns data, if you want Table1 to be modified you need to use a Make Table Query, you could then create the new table, remove old Table1 and then rename the new one.
    Last edited by Arkadi; 05-11-2016 at 08:17 AM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Registered User
    Join Date
    02-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    17

    Re: access query design help - adding field while keeping all records

    Quote Originally Posted by cytop View Post
    You cannot modify the structure of a table with a query (Generalisation ignoring DDL), but you can write a query to create a new table with the structure you want.

    If you need the original Table A modified and then updated, you can use VBA to modify the structure before running the query.

    Thanks for the reply. I tried creating a new table off of a query, but the result would be limiting the table to all cases where the common fields are matching. I'm actually looking for a result (table) where the common fields do not match. Is this possible through a query and not VBA? I'm not saying it shouldn't be done with VBA, but I have no experience with the coding of VBA in access.

    Thx.

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: access query design help - adding field while keeping all records

    To make a new table out of it:

    Select Table1.*, [Table2].[YesFieldName] INTO [Table2] FROM [Table1] LEFT JOIN [Table2] ON [Table1].[CommonFieldName] = [Table2].[CommonFieldName];

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: access query design help - adding field while keeping all records

    Sorry. I'm confused:
    where the two common fields matched
    (Post #1)

    where the common fields do not match
    (Post #4)

  7. #7
    Registered User
    Join Date
    02-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    17

    Re: access query design help - adding field while keeping all records

    Quote Originally Posted by Arkadi View Post
    To make a new table out of it:

    Select Table1.*, [Table2].[YesFieldName] INTO [Table2] FROM [Table1] LEFT JOIN [Table2] ON [Table1].[CommonFieldName] = [Table2].[CommonFieldName];
    This worked. Wow, thanks for the help! Much appreciated. Now that you showed me the solution, I hope I can learn what's actually happening in this code!

    Thx again to Arkadi and cytop for the help.

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: access query design help - adding field while keeping all records

    LEFT JOIN takes all data from first table and only matching from second table, then any fields from the second table will be null where there was no match between the two. An INNER JOIN gives only results where both tables have a match. And an OUTER JOIN would return data present in either Table 1 or Table 2, with blanks for all non-common fields where there was no match.

  9. #9
    Registered User
    Join Date
    02-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    17

    Re: access query design help - adding field while keeping all records

    Quote Originally Posted by cytop View Post
    Sorry. I'm confused:

    (Post #1)


    (Post #4)
    Sorry for any confusion. I was trying to add a field into Table A from Table B, but when I was running the make table query the tables were only showing records that matched Table B. I was trying to keep all of Table A's records intact while adding a field from Table B. Ultimately I only will be using data where Table B's field does not match Table A's.

    Again, thx for the suggestions and sorry about any confusion!

  10. #10
    Registered User
    Join Date
    02-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    17

    Re: access query design help - adding field while keeping all records

    Quote Originally Posted by Arkadi View Post
    LEFT JOIN takes all data from first table and only matching from second table, then any fields from the second table will be null where there was no match between the two. An INNER JOIN gives only results where both tables have a match. And an OUTER JOIN would return data present in either Table 1 or Table 2, with blanks for all non-common fields where there was no match.
    Thx, Arkadi! The JOIN functions seem very useful for my needs. I appreciate the explanation!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Looping through email body to include all records in query in Access.
    By SGT in forum Access Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2015, 09:39 AM
  2. Replies: 2
    Last Post: 05-06-2015, 09:24 AM
  3. Query Access database and return all records to Excel
    By bfs3 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2014, 11:01 PM
  4. [SOLVED] How can Access form run a query if field is not null?
    By jgomez in forum Access Tables & Databases
    Replies: 10
    Last Post: 01-14-2013, 02:35 PM
  5. Replies: 1
    Last Post: 10-19-2012, 02:08 PM
  6. Access Table Query to Combine Records
    By smoothlarryhughes in forum Access Tables & Databases
    Replies: 4
    Last Post: 09-14-2012, 08:45 PM
  7. MS Access Query Identify Friday's records.
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-03-2006, 03:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1