简体   繁体   中英

Update table records with VBA SQL on Access with referenced tables

I'm trying to update tblMAIN records from tblIMPORT records. The data on tblIMPORT needs to pull references from other tables to match the actual expected records on tblMAIN. The other tables are tblEMPLOYEES and tblTAXRATES. We tried doing it via a SQL query, but seems we can't quite get the query right. None of us knows how to construct this correctly since the person helping me is a webdev and uses MySQL and his query isn't quite the same to what MS Access expects.

The imported data on tblIMPORT does not have all the same information as tblMAIN so has to reference tblEMPLOYEES and tblTAXRATES to be able to find the matching information such as:

  • tblIMPORT has [EmpID] which is then the equivalent to [SeqNumID] on tblEMPLOYEES where we need to pull [Employee Name] which is what is required in tblMAIN.
  • Similarly, tblIMPORT only has [TaxRegion] but we need to line it up with the corresponding [GST percent], [PST percent], [HST percent] and [IsCompound] on tblTAXRATES that is required on tblMAIN.

Here's what we wrote:

' Update Main Entry Table from Imported Content
DoCmd.RunSQL "UPDATE tblMAIN t " & _
                "INNER JOIN tblIMPORT x ON x.[IsDeleted] IS NULL AND x.[EntryID] = t.[EntryID] " & _
                "LEFT JOIN tblEMPLOYEES e ON x.[EmpID] = e.[SeqNumID] " & _
                "LEFT JOIN tblTAXRATES r ON x.[TaxRegion] = r.[TaxRegion] " & _
                "SET t.[Employee Name] = e.[Employee Name],t.[TaxRegion] = x.[TaxRegion],t.[EntryID] = x.[EntryID],t.[EntryDate] = x.[EntryDate],t.[GST percent] = r.[GST percent],t.[PST percent] = r.[PST percent],t.[HST percent] = r.[HST percent],t.[IsCompound] = r.[IsCompound];"

Actually, MS Access and MySQL are probably only two major RDBMS's that share the same support of UPDATE...JOIN (unlike other databases that support UPDATE...FROM like SQL Server and Postgres or UPDATE with subqueries like Oracle, DB2, and SQLite).

However, MS Access requires the frustrating feature for new users of enclosing joins in parentheses where more than one JOIN is used. And some ON clause expressions may need to be run in WHERE like IS NULL . Hopefully, Access team changes this in a suggested, future version to be true to ANSI-1992.

Therefore, simply adjust your query to work. Consider also saving the SQL as a stored query and call it in VBA with DoCmd.OpenQuery for various reasons: 1) queries with incorrect syntax cannot be saved; 2) the compiler saves best execution plan to efficiently run for complex queries such as multiple joins; and 3) avoid concatenation of quoted strings in VBA.

SQL (save as a new query object)

UPDATE ((tblMAIN t INNER JOIN tblIMPORT x ON x.[EntryID] = t.[EntryID])
        LEFT JOIN tblEMPLOYEES e ON x.[EmpID] = e.[SeqNumID])
        LEFT JOIN tblTAXRATES r ON x.[TaxRegion] = r.[TaxRegion]
SET t.[Employee Name] = e.[Employee Name],
    t.[TaxRegion]     = x.[TaxRegion],
    t.[EntryID]       = x.[EntryID],
    t.[EntryDate]     = x.[EntryDate],
    t.[GST percent]   = r.[GST percent],
    t.[PST percent]   = r.[PST percent],
    t.[HST percent]   = r.[HST percent],
    t.[IsCompound]    = r.[IsCompound]
WHERE x.[IsDeleted] IS NULL;

VBA (no need to close action queries)

DoCmd.OpenQuery "mySavedUpdateQuery"

For what I read here you will most likely need more than one query, so I strongly suggest you to instead of creating a single big update query, do the following

1-) Create a select query with the data from tblIMPORT and all join there all tables you can. 2-) Insert into tblMAIN the data from step #1 3-) Run an update query in tblMAIN, joining with any other table you need to add info.

It will simplify the process and help you to organize your ideas. By the end you will most likely be able to put the 3 steps above in 1 or 2 queries, but start simple.

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