Results 1 to 8 of 8
  1. #1
    CryptikFox is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    3

    Replace Value with IFF Function

    Hello,


    I am wondering what the best way to go about automating some values that need replacing in an access database.


    We have a table with over 50,000 entries, and dozens of parameters (columns). Three of them are important here, let's call them the A, B, and C columns.
    Columns A and B are all number values. Column C occasionally has a short text value for some rows, occasionally, the singular letter "U."
    For a given row, we would like to replace the value in column A, with the value in column B, IF AND ONLY IF the value in column C is "U."




    Would this have to be run as a query, or is there a way to make the original table itself do this? Either solution would work for us, but it'd be best if we could have one table with the original data, and a second table that is automatically generated with the replaced values.

    Thanks in advance for the help.

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    This could be done with a query (faster) or VBA - it is your choice.

    To duplicate the table (make a back up), right click on the table name and select "Copy". Then right click and select "Paste".

    Your query would looks something like:
    Code:
    UPDATE Table1 SET Table1.ColA =  Table1.ColB
    WHERE Table1.ColC ="U";
    Change names as appropriate.....


    VBA would take longer because you would have to step through each record (50,000) of the table.


    Warning: Make a back up of the database BEFORE trying this!!!! You have been warned......

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Or you could leave your original data in the table and create a new SELECT query in which you use IIF in the A and B columns: NEW_A:iif([C]="U","New_A_Value",[A]).

    Cheers,
    Vlad

  4. #4
    CryptikFox is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    3
    Hey ssanfu, I tried:

    Quote Originally Posted by ssanfu View Post
    Code:
    UPDATE Table1 SET Table1.ColA =  Table1.ColB
    WHERE Table1.ColC ="U";
    But keep getting a syntax "error (missing operator)."

    Click image for larger version. 

Name:	Access SQL Error.jpg 
Views:	15 
Size:	77.6 KB 
ID:	37137

    Bear with me... I'm a total noob when it comes to any sort of coding or advanced usage of Access.

    To clairify, my Columns A, B, and C are as follows:
    A=Result
    B=PQL
    C=Qualifier

    I want the "Result" value to be replaced with the "PQL" value if the "Qualifier" value is "U."

    Thanks again for the help.

  5. #5
    CryptikFox is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    3
    Quote Originally Posted by Gicu View Post
    Or you could leave your original data in the table and create a new SELECT query in which you use IIF in the A and B columns: NEW_A:iif([C]="U","New_A_Value",[A]).

    Cheers,
    Vlad
    Hey Vlad, I actually got your method to work... BUT we'd be running into another big issue if we did it this way. The problem is we have a bunch of other macros and queries that rely on the A column to be called A. With your method, the new A column needs to have a different name, otherwise there's a circular reference.

    I suppose the query can be copy pasted then "A_New" can be manually renamed back to "A," but that starts defeating the purpose of the automation. Let me know if you have any thoughts on this, and thanks for the help.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Can't have those SELECT and UPDATE statements in the same query object.

    UPDATE action would make a permanent change of values in field. Do you really want that?

    Might just have to do the IIf replacement calc in those other macros and queries. How many is a 'bunch'?
    Last edited by June7; 01-26-2019 at 05:20 PM.
    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.

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Be aware that "Data" and "Result" are reserved words and shouldn't be used as object names.

    Here is a dB to demonstrate what you want to do.
    Open the table "Data" and look at the values.
    Close the table, then execute the query.
    Open the table again and check the field values.
    Attached Files Attached Files

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    If you fully qualify the name for the fields in the expression you don't need to assign a new name to the field; have a look at the attached sample and you see that I renamed the original table tblResults_Orig and created a new query called tblResults that has the calculated field. In most cases the new query can be used exactly like the table it replaces, including code (so in VBA set rst=currentdb.openrecordset("tblResults", dbOpendynaset) would still work as expected).

    I'm not saying it is the best approach from a db design point of view but sometimes we need to take shortcuts....

    Cheers,
    Vlad
    Attached Files Attached Files

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

Similar Threads

  1. Replace Function
    By razkowski in forum Queries
    Replies: 7
    Last Post: 10-08-2014, 02:28 PM
  2. Using a wildcard with the replace function
    By razkowski in forum Queries
    Replies: 6
    Last Post: 08-12-2014, 08:13 AM
  3. Replace Function
    By thescottsman92 in forum Access
    Replies: 5
    Last Post: 09-02-2013, 01:25 AM
  4. Help with Replace Function...
    By redbull in forum Programming
    Replies: 5
    Last Post: 06-27-2013, 04:05 PM
  5. Replies: 3
    Last Post: 06-07-2012, 07:05 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