Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL syntax error 3

Status
Not open for further replies.

merlynsdad

Programmer
Nov 18, 2010
175
US
I have a query qryTotalRdyTm with two fields, SEID and ReadyTime, on which I am summing. I have a table called tblSEIDtoExt_Num with a date/time field called TotalRdyTm. I'm trying to move the result of the query to that field using the following SQL (in VBA):

strSQL = "UPDATE tblSEIDtoExt_Num " _
& "SELECT Sum(tblSEIDtoExt_Num.ReadyTime) AS SumOfReadyTime " _
& "FROM qryTotalRdyTm " _
& "SET tblSEIDtoExt_Num.TotalRdyTm = qryTotalRdyTm.SumOfReadyTime " _
& "WHERE SEID= '" & strSEID & "';"

I'm getting a syntax error and can't find the problem.

If the square peg won't fit in the round hole, sand off the corners.
 
I assume you have a legitimate reason to store a value that can be calculated at any time.

Any query with a Sum() will not be updateable. You might need either code to edit a record in a recordset or use DSum() to update a field.
Code:
strSQL = "UPDATE tblSEIDtoExt_Num " & _
  "SET TotalRdyTm = DSum('ReadyTime','tblSEIDToExt_Num','SEID='" & _
  strSEID & "')"
  debug.Print strSQL  [green]'Find out the full SQL[/green]

Duane
Hook'D on Access
MS Access MVP
 
try something like this
put this string in to SqlStr

Code:
update tblSEIDtoExt_Num 
inner join (
            SELECT Sum(tblSEIDtoExt_Num.ReadyTime) 
            AS SumOfReadyTime 
            FROM qryTotalRdyTm 
            Group by strSEID
            Where strSEID =" & strSEID & _
            ) As SOfRT
on SOfRT.strSEID =tblSEIDtoExt_Num.strSEID 
SET tblSEIDtoExt_Num.TotalRdyTm = SOfRT.SumOfReadyTime
 
PWise,
I think this is still not updateable because of the totals query in the subquery. Other flavors of SQL might allow this but Access typically doesn't.

I tried something similar in Northwind just to be sure. I added a ProductCount field in the Categories table. I wanted to update the field to the number of products in the category. This SQL gets me the count:
Code:
SELECT Products.CategoryID, Count(Products.ProductID) AS CountOfProductID
FROM Products
GROUP BY Products.CategoryID
so I thought this might match your update syntax:
Code:
UPDATE Categories INNER JOIN (SELECT Products.CategoryID, Count(Products.ProductID) AS CountOfProductID
FROM Products
GROUP BY Products.CategoryID) As PrdCnt
ON Categories.CategoryID = PrdCnt.CategoryID
SET ProductCount = PrdCnt.CountOfProductID;
I got the error message:
Operation must use an updateable query
This query does work:
Code:
UPDATE Categories SET Categories.ProductCount = DCount("*","Products","CategoryID =" & [CategoryID]);



Duane
Hook'D on Access
MS Access MVP
 
Duane
you are right as usual

This works in SQLServer

have another Star
 
I wish we had some of the SQL Server functionality available in Access such as your syntax, CTE, and many others. Of course, SQL Server should add a simple crosstab like Access.

Duane
Hook'D on Access
MS Access MVP
 
For someone who's new to Access VBA & SQL (background is Excel & Word VBA) it's certainly confusing! I do appreciate the quick responses from you guys, though it seems I have a new question for every response.

If the square peg won't fit in the round hole, sand off the corners.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top