Results 1 to 3 of 3
  1. #1
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107

    Update Only Where Value Exists

    I am trying to create an update statement that will update a field only where there is a value in another field. My table structures are as such:


    PRODUCT STATUS TABLE

    Product Name Product Number Product Status


    TEMP PRODUCT STATUS CHANGE TABLE


    Product Name Product Number Product Status Product Status Change


    I want to update the PRODUCT STATUS TABLE.Product Status to the TEMP PRODUCT STATUS CHANGE TABLE.Product Status Change, but only where there actually is a product status change. This is the update query I have created:

    Code:
    UPDATE [TEMP PRODUCT STATUS CHANGE TABLE] RIGHT JOIN [PRODUCT STATUS TABLE] ON [TEMP PRODUCT STATUS CHANGE TABLE].[Product Number] = [PRODUCT STATUS TABLE].[Product Number] 
    SET [PRODUCT STATUS TABLE].[Product Status] = [TEMP PRODUCT STATUS CHANGE TABLE].[Product Status Change]
    WHERE ((([TEMP PRODUCT STATUS CHANGE TABLE].[Product Status Change]) Is Not Null));
    When I run this code, the [PRODUCT STATUS TABLE].[Product Status] field is updated to the [TEMP PRODUCT STATUS CHANGE TABLE].[Product Status Change] field, but all records are updated. I need only the records that have a product status change to be updated. Is there a way to change my update query so only the status changed records become updated?


    Thank you for your time and help!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Are the records in your tables unique?

    Try this, but back up your table first.

    Code:
    UPDATE [PRODUCT STATUS TABLE] INNER JOIN [TEMP PRODUCT STATUS CHANGE TABLE] ON 
    [PRODUCT STATUS TABLE].[Product Number] = [TEMP PRODUCT STATUS CHANGE TABLE].[Product Number] 
    SET [PRODUCT STATUS TABLE].[Product Status] = [TEMP PRODUCT STATUS CHANGE TABLE].[Product Status Change]
    WHERE ((([TEMP PRODUCT STATUS CHANGE TABLE].[Product Status Change]) Is Not Null));

  3. #3
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    Thank you for your response Orange, and I apologize for the delay in mine.

    And thank you for providing the above query. Unfortunately, I am experiencing the same difficulties with your query as I am with mine. Running your query will update the data that I need, but will also update the data that I do not want. Perhaps some example data will be a little more clear.

    PRODUCT STATUS TABLE
    Product name Product Number Product Status
    Incredible 9000 12345 Green
    Kirk 67890 Yellow
    Tardis 111009 Red
    Pond 66453 Green



    TEMP PRODUCT STATUS CHANGE TABLE
    Product name Product Number Product Status Product Status Change
    Incredible 9000 12345 Green
    Kirk 67890 Yellow Green
    Tardis 111009 Red
    Pond 66453 Green Red

    I need the end result to look like:
    PRODUCT STATUS TABLE
    Product name Product Number Product Status
    Incredible 9000 12345 Green
    Kirk 67890 Green
    Tardis 111009 Red
    Pond 66453 Red

    Where only the product status in bold change. Currently, the two queries that have been presented change the data to look as such:
    PRODUCT STATUS TABLE
    Product name Product Number Product Status
    Incredible 9000 Green
    Kirk 67890 Green
    Tardis 111009
    Pond 66453 Red

    Changing the data where it is needed, but leaving the other statuses as null.

    Is there anyway to make the data appear as I need it?

    Thank you!

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

Similar Threads

  1. if exists UPDATE else INSERT
    By lloyddobler in forum Programming
    Replies: 18
    Last Post: 01-04-2012, 11:35 PM
  2. If table exists then
    By SorenIX in forum Programming
    Replies: 2
    Last Post: 06-26-2011, 08:42 AM
  3. Not Exists Between Query
    By Pells in forum Queries
    Replies: 5
    Last Post: 11-08-2010, 06:13 AM
  4. Add columns if not exists
    By ysrini in forum Access
    Replies: 1
    Last Post: 02-16-2010, 06:39 PM
  5. How to use if not exists in access?
    By halifaxdalRaymondXie in forum Queries
    Replies: 1
    Last Post: 01-14-2010, 03:18 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