Results 1 to 7 of 7
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038

    Update query with six (6) IIF update rules... please help to identify most efficient way processing

    Experts:

    I need some assistance with writing an UPDATE query based on multiple conditions.

    Background first...
    1. Attached DB contains two (2) tables: [01_tbl_Before] and [02_tbl_After] where "02" is a current copy of "01"
    2. [01_tbl_Before] contains 2 fields: [T1] and [T2]

    Please see below the six (6) business rules (BR) that need to be integrated into an UPDATE query.

    1. IIF ([T1] = 15 AND [T2]) = 15 THEN [T2] = NULL
    2. IIF ([T1] = 15 AND [T2]) = (100 through and incl. 120) THEN [T1] = 10
    3. IIF ([T1] = 15 AND [T2]) = (130 through and incl. 160) THEN [T1] = 11
    4. IIF ([T1] = 15 AND [T2]) = (170 through and incl. 190) THEN [T1] = 12
    5. IIF ([T1] = 15 AND [T2]) = (200 through and incl. 230) THEN [T1] = 13
    6. IIF ([T1] = 15 AND [T2]) = (240 through and incl. 275) THEN [T1] = 14



    I presume the above BR are (I hope self-explanatory) and don't require further information. If needed, however, I'll gladly expand on them.

    Here's what I need some help with:
    Although I have created IIF statements before and then used them as an UPDATE query, I'm not 100% confident that *stringing* these 6 BRs into a single IIF statement is the most efficient way to update my records.

    Thus, my question: What is the most efficient way to update those records (either field [T1] and/or [T2]) where the conditions of the 6 BRs are met?

    Thank you,
    Tom
    Attached Files Attached Files

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Tom,

    Does it have to be a query?

    Code:
    '1. IIF ([T1] = 15 AND [T2]) = 15 THEN [T2] = NULL
    '2. IIF ([T1] = 15 AND [T2]) = (100 through and incl. 120) THEN [T1] = 10
    '3. IIF ([T1] = 15 AND [T2]) = (130 through and incl. 160) THEN [T1] = 11
    '4. IIF ([T1] = 15 AND [T2]) = (170 through and incl. 190) THEN [T1] = 12
    '5. IIF ([T1] = 15 AND [T2]) = (200 through and incl. 230) THEN [T1] = 13
    '6. IIF ([T1] = 15 AND [T2]) = (240 through and incl. 275) THEN [T1] = 14
    
    Sub testing()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Set db = CurrentDb
        Set rs = db.OpenRecordset("01_tbl_before")
        Do While Not rs.EOF
            rs.Edit
            Debug.Print rs!t1 & "  " & rs!t2
            If rs!t1 = 15 Then
                Select Case rs!t2
                    Case 15
                        rs!t2 = Null
                    Case 100 To 120
                        rs!t1 = 10
                    Case 130 To 160
                        rs!t1 = 11
                    Case 170 To 190
                        rs!t1 = 12
                    Case 200 To 230
                        rs!t1 = 13
                    Case 240 To 275
                        rs!t1 = 14
                End Select
            End If
            rs.Update
            rs.MoveNext
        Loop
    
    
    End Sub

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Orange... wow... this is great. I definitely like this approach much better than a query.

    Two things:
    I'm only updating [T2], so I changed the rs!t1 to rs!t2.
    Next, some values (e.g., 200, 205, 210, 215, 220, 225) are not converted into the 2-digit number. I'll have a closer look to figure out why those values (a total of 357 values out of 2,444 values) are NOT converted.

    Again, while I need to determine why 357 values are not picked up for conversion, I definitely love this process!!!

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Tom,

    I had an error in the original code, I since updated it

    Check that this is in your test set up. I had a dash in the original

    Case 200 To 230
    rs!t1 = 13

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    OP has a result
    Last edited by CJ_London; 07-07-2021 at 02:43 PM. Reason: OP has a result

  6. #6
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Orange -- per post #5 you were correct about the T1/T2 conversion. The BRs specified in the original post are correct.

    I definitely prefer the module (over a query)... 'much easier to add/modify if the BRs change and/or expand. Thank you for this function... 'very much appreciated!!

  7. #7
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Jack -- good morning... I posted a new (related) thread a moment ago.

    I certainly would welcome your feedback...

    https://www.accessforums.net/showthr...308#post479308

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

Similar Threads

  1. Update query rules error
    By Jen0dorf in forum Access
    Replies: 2
    Last Post: 03-08-2016, 09:36 AM
  2. Replies: 17
    Last Post: 05-07-2015, 11:14 AM
  3. Replies: 5
    Last Post: 10-24-2013, 04:38 PM
  4. Replies: 7
    Last Post: 08-26-2013, 06:05 AM
  5. Replies: 2
    Last Post: 08-30-2012, 07:59 AM

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