简体   繁体   中英

How to update table in MS Access 2007

I'm trying to update records in a table in MS Access 2007 from a temporary table. The temporary table gets new data from an external tool. The new data (2 columns) is supposed to go into the persistent table.

I tried the 2 following queries, but both show empty records when I look at the Datasheet view of the Query. (btw is there a better way to debug SQL in access?) The second Query also shows as many rows as there are in table1 instead of only the amount of rows edited by the external tool. (in both cases, the results are empty for every row)

the version that has at least the right amount of records:

UPDATE (table1 INNER JOIN table2 ON table1.key = table2.key)
SET table1.value1 = table2.value1, table1.datevalue = table2.datevalue
WHERE ((table1.value1) IS NULL);

the version with as many records as result as there are records in table1:

UPDATE table1
SET table1.value1 = (SELECT value1
FROM table2
WHERE ((table1.value1 IS NULL)
AND (table1.key = table2.key))),
table1.datevalue = (SELECT datevalue
FROM table2
WHERE ((table1.key = table2.key)
AND (table1.value1 IS NULL)))

In my test the tool manipulated 1 record so I expected to see one row as Output of the query with value1 and datevalue from table2 , yet the output shows value1 and datevalue with empty entries (1 or (amount of records in table1 )).

show empty records when I look at the Datasheet view of the Query

If you openn Update query in table mode, you will see only current values of the cells. You should run (execute) your query in order to get values updated. Update query cannot be checked for preview (as you want, as I understood).

To fully answer your questions, action queries including UPDATE and INSERT in the MS Access .exe program will raise a message prompt by default to inform the user how many rows will be updated or inserted and if user wants to continue with the action. Other views of action queries may not inform affected records.

访问GUI消息

This message will pop up if action query is called from:

If not using MS Access .exe (since Access is both an application program and a database), you can still see how many records are affecting via code using DAO's Database.RecordsAffected property which is part of the MS Access object library. Any COM-interfaced language can run the same process including VBA, Python, PHP, Java, etc.

VBA Example (note: VBA is a separate component to MS Access and other Office apps)

Sub RunUpdate()    
   Dim db as DAO.Database

   Set db = CurrentDb()
   db.Execute "mySavedUpdateQuery"

   MsgBox CStr(db.RecordsAffected) & " records were affected."    

   Set db = Nothing
End Sub

Python Example

import os
import win32com.client

access_file = r'C:\Path\To\MyDatabase.accdb'

try:
    oApp = win32com.client.Dispatch("Access.Application")
    oApp.OpenCurrentDatabase(access_file)

    db = oApp.Currentdb()
    db.Execute("EmployeesUpdateQ")
    print("{} records were affected.".format(db.RecordsAffected))

except Exception as e:
    print(e)

finally:
    oApp.Quit()
    db = None; oApp = None
    del db; del oApp

Number of Rows Question

Both your queries will update different number of records. First version maintains a JOIN and WHERE condition on outer level so will affect a filtered number of records in table1 .

UPDATE (table1 t1 INNER JOIN table2 t2 ON t1.[key] = t2.[key])
SET t1.value1 = t2.value1, 
    t1.datevalue = t2.datevalue
WHERE ((t1.value1) IS NULL);

Second version uses subqueries with JOIN and WHERE conditions but nothing on outer level and so all records of table1 will be affected but specific values may update to NULL . In fact, you may receive an exception error if subqueries return more than one value:

UPDATE table1
SET table1.value1 = (SELECT value1
                     FROM table2
                     WHERE ((table1.value1 IS NULL)
                       AND  (table1.[key] = table2.[key]))),

    table1.datevalue = (SELECT datevalue
                        FROM table2
                        WHERE ((table1.[key] = table2.[key])
                          AND  (table1.value1 IS NULL)))

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