Results 1 to 3 of 3
  1. #1
    luckycharms is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    8

    Update fields with complicated algorithim: SQL or VB?


    Hi Folks,

    I need to update a foreign key column in a table. The data in my two tables is somewhat messy, and hence my query needs to be somewhat complicated. I'm not sure if it's better done in SQL or VB, and in either case, how. Here's what I need to do in psedo-code:

    Code:
    UPDATE table_1 t1, table_2 t2
        IF t1.col1 NOT = "string1" or "string2" THEN
            SET t1.t2ID = t2.ID WHERE
                t1.match1 = t2.match1
        ELSE
            SET t1.t2ID = t2.ID WHERE
                t1.match2 = t2.match2
    I probably would prefer to do this via VB, as it might afford more flexibility into the future as I need to tweak things. But, my ears are wide open. Be it via VB or SQL, how might I do this?

    Thanks a ton!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,942
    I don't quite understand the WHERE clauses. Don't know if that can be dynamic in Access query object.

    UPDATE table_1 As t1 INNER JOIN table_2 As t2 ON t1.ID = t2.ID SET t1.t2ID = t2.ID WHERE IIf(t1.col1 <> "string1" or t1.col1 <> "string2", t1.match1=t2.match1, t1.match2=t2.match2)

    What has to be 'tweaked'? If you want the string1 and string2 to be dynamic, they can be input parameters in the query. VBA code could run the sql:

    CurrentDb.Execute "UPDATE ..."
    Last edited by June7; 06-08-2012 at 05:44 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.

  3. #3
    luckycharms is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    8
    Thanks for the reply.

    Here's what I've ended up implementing in VB. I imagine there are better ways of going about it, but this works for me. Thanks for your help!

    Sub Link_table1()

    CurrentDb.Execute "UPDATE table1 AS t1 SET t1.t2ID=NULL"

    ' Join on col1 and match1 when match1 meets a criterion

    CurrentDb.Execute "UPDATE table1 AS t1, Table2 AS t2 SET " _
    & "t1.t2ID = t2.ID WHERE t1.col1 = t2.col1 AND t1.match1 = t2.match1 AND t1.match1 = ""string1"""

    ' Join on col1 and match2 when match1 doesn't meet the criterion

    CurrentDb.Execute "UPDATE table1 AS t1, Table2 AS t2 SET " _
    & "t1.t2ID = t2.ID WHERE t1.col1 = t2.col1 AND t1.match2 = t2.match2 " _
    & "AND t1.match1 <> ""string1"""

    End Sub

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

Similar Threads

  1. Complicated
    By Ganymede in forum Queries
    Replies: 3
    Last Post: 01-22-2012, 06:25 PM
  2. Complicated form
    By secret in forum Access
    Replies: 14
    Last Post: 09-07-2011, 10:16 PM
  3. Complicated IIF?
    By Sweetnuff38 in forum Queries
    Replies: 1
    Last Post: 08-18-2011, 01:13 PM
  4. Replies: 5
    Last Post: 03-20-2010, 08:30 AM
  5. Complicated ASP SQL to Access db
    By KLynch0803 in forum Programming
    Replies: 0
    Last Post: 01-31-2010, 08:32 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