简体   繁体   中英

Update using two tables in Ms access vba

I want to update a table using numbers from another table, I want to do the following formla

YX(1)-(A1+A2+A3)

X is form table1 while Y,A1,A2,A3 from table2. The new record wil be updated in column X where the ID has only one of the records = 0

Table1

    ID   ||  X
    -------------
    1    ||  **0**
    1    ||  155
    2    ||  4
    3    ||  0
    3    ||  0
    3    ||  234
    4    ||  0
    4    ||  0

Table2

ID   ||  Y   ||  A1   ||  A2   ||  A3
--------------------------------------
1    ||  228 ||  1    ||  3    ||  4
2    ||  112 ||  6    ||  7    ||  7    
3    ||   4  ||  22   ||  1    ||  0
4    ||  78  ||  76   ||  6    ||  2

from the above example tables the only column that will match the certiria is ID=1 where only one of them is 0 and the count = 2 so column X where =0 will be updated ( X2= 228-155-(1+3+4))

Updated table1

ID   ||  X
-------------
1    ||  **65**
1    ||  155
2    ||  4
3    ||  0
3    ||  0
3    ||  234
4    ||  0
4    ||  0

My code is

Private Sub GET_TWO_INJLINE_EST()

  DoCmd.SetWarnings False
  DoCmd.RunSQL "UPDATE table1 I, table2 P" _
                 & " SET I.X = IIf(I.X = 0, DLookup(""P.Y-I.X-(P.A1 + P.A2 + P.A3)"" , " _
                 & "                                ""table1"", ""[ID]="" & [ID] & "" AND I.X <> 0""), I.X)" _
                 & " WHERE I.[ID] IN (SELECT I.[ID] FROM table1 I" _
                 & "                  GROUP BY I.[ID] HAVING (Count(I.[ID]) = 2)" _
                 & "                  AND (Min(I.ID) <> Max(I.X))" _
                 & "                  AND (Min(I.X) = 0 OR Max(I.X) = 0))" _
                 & " AND I.ID=P.ID"
  DoCmd.SetWarnings True

End Sub

when I run it show me "Unknown" error.

try changing the second table1 I to table1 J and update all the references to the second table1

UPDATE table1 I, table2 P

    Set I.X = IIf(I.X = 0,
                  DLookup(""P.Y-I.X-(P.A1 + P.A2 + P.A3)"",
                          ""table1"",
                          ""[ID]="" & [ID] & "" AND I.X <> 0""
                         ),
                  I.X
                 )

     WHERE I.[ID] IN (
        SELECT J.[ID] FROM table1 J
        GROUP BY J.[ID]
        HAVING (Count(J.[ID]) = 2)
           AND (Min(J.ID) <> Max(J.X))
           AND (Min(J.X) = 0 OR Max(J.X) = 0))
           AND J.ID=P.ID

Consider an UPDATE ... INNER JOIN query and reconciling your DLookUp as you attempt to pass IX within the string expression. Furthermore, your subquery's Min() <> Max() equality does not compare to same X.

Finally, consider saving the update query as a stored Access query without concatenating in a VBA string which avoids the double quoting and forces you to check syntax error prior to saving. Plus, the database engine caches and pre-compiles for best execution plan.

SQL (save as Access stored query; see DMax inside larger expression)

UPDATE table1 I
INNER JOIN table2 P ON I.ID = P.ID   

SET I.X = IIF(I.X=0, (P.Y - DMax("X", "table1", "ID=" & I.ID) - (P.A1+P.A2+P.A3)), I.X)    
WHERE I.[ID] IN 
             (SELECT sub.[ID] 
              FROM table1 sub
              GROUP BY sub.[ID] 
              HAVING (Count(sub.[ID]) = 2)
                AND (Min(sub.X) <> Max(sub.X))
                AND (Min(sub.X) = 0 OR Max(sub.X) = 0))

VBA

Private Sub GET_TWO_INJLINE_EST()    
  DoCmd.SetWarnings True
  DoCmd.OpenQuery "mySavedUpdateQuery"
  DoCmd.SetWarnings True    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM