By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,493 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,493 IT Pros & Developers. It's quick & easy.

Expected End of Statement thwarting attempt to update multiple tables

P: 25
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 (qryEqiupEmplOnly), 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?

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub AssetUpdate()
  4.   Dim db As DAO.Database, rstName As DAO.Recordset, SQL As String
  5.   Dim n As Integer, Table As String
  6.  
  7.    Set db = CurrentDb
  8.    Set n = 1
  9.  
  10.    'Setup Recordset
  11.    SQL = "SELECT [Employee Name], SerialNo, AssetNo, Category " & _
  12.          "FROM qryEquipEmplOnly " & _
  13.          "ORDER BY [Employee Name];"
  14.    Set rstName = db.OpenRecordset(SQL, dbOpenDynaset)
  15.  
  16.    'Parse thru tables & update asset numbers
  17.    Do Until rstName.EOF
  18.       Table = Switch(rstName!Category = "Computers", "tblComputers", _
  19.             rstName!Category = "Cameras", "tblDECameras", _
  20.             rstName!Category = "Docks", "tblDEDocks", _
  21.             rstName!Category = "MFCs", "tblDEMFCs", _
  22.             rstName!Category = "Monitors", "tblDEMonitors", _
  23.             rstName!Category = "Printers", "tblDEPrinters", _
  24.             rstName!Category = "Other DE", "tblDEOthers")
  25.  
  26.       SQL = "UPDATE " & Table & " " & _
  27.             "SET AssetNo = n " & _
  28.             "WHERE [Employee Name] = '" rstName![Employee Name] "' "& _
  29.             "AND SerialNo = '" & rstName!SerialNo & "';"
  30.       db.Execute SQL, dbFailOnError
  31.       Set n = n + 1
  32.       rstName.MoveNext
  33.    Loop
  34.  
  35.    Set rstName = Nothing
  36.    Set db = Nothing
  37. End Sub
  38.  
  39.  
Apr 23 '08 #1
Share this Question
Share on Google+
4 Replies


Expert Mod 2.5K+
P: 2,545
Hi. You are missing two '&' operators in line 28. You also have an error in line 27, where you have accidentally put the reference to variable 'n' inside your SQL string. Revised lines shown below.

-Stewart

Expand|Select|Wrap|Line Numbers
  1. "SET AssetNo = " & n & _
  2. " WHERE [Employee Name] = '" & rstName![Employee Name] & "' "& _
  3.  
Apr 23 '08 #2

P: 25
Thank you Stewart. That did fix the problem I described above. However, I've run into a different problem. When I try running the code I an error "3061", too few parameters: expected 1. I've determined that it has to do with the Table parameter. Either I'm not getting the value I want or the query the recordset calls is too complex to run. (It's a qruery filtering out one employee name from a previous union query.)

Here's the modified code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AssetUpdate()
  2.   Dim db As DAO.Database, rstName As DAO.Recordset, SQL As String
  3.   Dim n As Integer, Table As String
  4.  
  5.    Set db = CurrentDb
  6.    n = 1
  7.  
  8.    'Setup Recordset
  9.    SQL = "SELECT [Employee Name], SerialNo, AssetNo, Category " & _
  10.          "FROM qryEquipEmplOnly " & _
  11.          "ORDER BY [Employee Name];"
  12.    Set rstName = db.OpenRecordset(SQL, dbOpenDynaset)
  13.  
  14.    'Parse thru tables & update asset numbers
  15.    Do Until rstName.EOF
  16.       Table = Switch(rstName!Category = "Computers", "tblComputers", _
  17.             rstName!Category = "Cameras", "tblDECameras", _
  18.             rstName!Category = "Docks", "tblDEDocks", _
  19.             rstName!Category = "MFCs", "tblDEMFCs", _
  20.             rstName!Category = "Monitors", "tblDEMonitors", _
  21.             rstName!Category = "Printers", "tblDEPrinters", _
  22.             rstName!Category = "Other DE", "tblDEOthers")
  23.  
  24.       SQL = "UPDATE " & Table & _
  25.             " SET [AssetNo] = " & n & _
  26.             " WHERE [Employee Name] = '" & rstName![Employee Name] & "' " & _
  27.             "AND [SerialNo] = '" & rstName!SerialNo & "';"
  28.       db.Execute SQL, dbFailOnError
  29.       n = n + 1
  30.       rstName.MoveNext
  31.    Loop
  32.  
  33.    Set rstName = Nothing
  34.    Set db = Nothing
  35. End Sub
  36.  
  37.  
Apr 24 '08 #3

Expert Mod 2.5K+
P: 2,545
Hi. I am assuming that the failure point is at the OpenRecordset line...

Could you check what the Table and SQL variables are set to and post these back so that the syntax of what is actually being passed to OpenRecordset can be seen? Set a breakpoint within your code, and step through each line one by one checking the values of the SQL string and the Table variable as you go.

Just as an aside, is your SerialNo field text? You are enclosing the reference to the field in single quotes, which is fine as long as it is a text field. If it is numeric then the single quotes are incorrect and should be removed.

-Stewart

ps Looking again at the Where clause in your update, does it really belong to the tables that are listed in your Switch statement? It would be unusual to have employee names in such tables, so I wonder if your Where clause actually relates to query qryEquipEmplOnly instead??? If this is the case you need to completely rethink your Update statement as it cannot work if the Where clause has no relation to the table being updated.
Apr 24 '08 #4

P: 25
Stewart,

It turned out that I needed to write a more complex SQL statement, which I did get to work:

Expand|Select|Wrap|Line Numbers
  1.       SQL = "UPDATE tblEmployees INNER JOIN " & Table & _
  2.             " ON tblEmployees.EmployeeID = " & Table & ".EmployeeID" & _
  3.             " SET " & Table & ".AssetNo = " & n & _
  4.             " WHERE (((tblEmployees.[Employee Name]) = '" & rstName![Employee Name] & "') " & _
  5.             "AND ((" & Table & ".SerialNo) = '" & rstName!SerialNo & "'));"
  6.       db.Execute SQL, dbFailOnError
  7.  
The old SQL statemnet was looking for [Employee Name] when the Table had a field [EmployeeID] instead. The [Employee Name] field was actually coming from the tblEmpoyees table, so I had to make the necessary adjustment.

With the new SQL statment it worked beautifullly.

Thanks for your help!
Apr 24 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.