I'm trying to run an update query on multiple tables, and since Access doesn't allow me to update tables from a union query, I'm writing a module as a workaround. So I've set up a temporary recordest (rstName) from the union query (qryEqiupEmplOn ly), and construced a Do-Loop that updates the corresponding table.
Unfortunately the Update SQL code prompts a compile error "Expected End of statement" and highlights "rstName" (line 28) as the culprit. Does anyone know how to fix it?
Unfortunately the Update SQL code prompts a compile error "Expected End of statement" and highlights "rstName" (line 28) as the culprit. Does anyone know how to fix it?
Code:
Option Compare Database
Private Sub AssetUpdate()
Dim db As DAO.Database, rstName As DAO.Recordset, SQL As String
Dim n As Integer, Table As String
Set db = CurrentDb
Set n = 1
'Setup Recordset
SQL = "SELECT [Employee Name], SerialNo, AssetNo, Category " & _
"FROM qryEquipEmplOnly " & _
"ORDER BY [Employee Name];"
Set rstName = db.OpenRecordset(SQL, dbOpenDynaset)
'Parse thru tables & update asset numbers
Do Until rstName.EOF
Table = Switch(rstName!Category = "Computers", "tblComputers", _
rstName!Category = "Cameras", "tblDECameras", _
rstName!Category = "Docks", "tblDEDocks", _
rstName!Category = "MFCs", "tblDEMFCs", _
rstName!Category = "Monitors", "tblDEMonitors", _
rstName!Category = "Printers", "tblDEPrinters", _
rstName!Category = "Other DE", "tblDEOthers")
SQL = "UPDATE " & Table & " " & _
"SET AssetNo = n " & _
"WHERE [Employee Name] = '" rstName![Employee Name] "' "& _
"AND SerialNo = '" & rstName!SerialNo & "';"
db.Execute SQL, dbFailOnError
Set n = n + 1
rstName.MoveNext
Loop
Set rstName = Nothing
Set db = Nothing
End Sub
Comment