Solved

SQL Update Query with Summed Field VB6 MS Access 2003

Posted on 2011-09-02
8
437 Views
Last Modified: 2012-05-12
Hello experts.  I am working with VB6 and MS Access 2003.  This code produces the error ‘syntax error in query expression’.   I’d greatly appreciate any advice.      

Dim strSQL6 As String
strSQL6 = "Update [ProjOverview] Set [Status] = 'Complete' and [Complete] = Now AND ([TotalServicesAct] = [TotalServicesEst]) Where (([Status] = 'Canceled') AND ([TotalCost] > 0) AND (SELECT Sum[JONProjJoins].[Amount]) AS [SumOfAmount], [JONProjJoins].[ProjID] FROM [ProjOverview] LEFT JOIN [JONProjJoins] ON [ProjOverview].[IDNmbr] = [JONProjJoins].[ProjID] GROUP BY [JONProjJoins].[ProjID] Having Sum([JONProjJoins].[Amount]) < 0 Or Sum(JONProjJoins.Amount) = 0"
DoCmd.RunSQL strSQL6

The goal of the update query is:
Update ProjOverview table
Set
1)      Status = Complete
2)      Complete = Now
3)      TotalServicesAct = TotalServicesEst
Where
1)      Status = Canceled      
2)      TotalCost > 0  
3)      LinkedFunds < = 0
The LinkedFunds field is a summed amount from JONProjJoins table
0
Comment
Question by:KimD2
  • 4
  • 2
  • 2
8 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36474381
the structure is using "," not "AND" between set elements

Update [ProjOverview]
Set
[Status] = 'Complete',
[Complete] = Now ,
[TotalServicesAct] = [TotalServicesEst]
...
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 36474431
I am not sure but this query is correct (may not be logically correct ;)
Update [ProjOverview] 
Set 
[Status] = 'Complete',
[Complete] = Now ,
[TotalServicesAct] = [TotalServicesEst]
Where (
([Status] = 'Canceled') AND 
([TotalCost] > 0) AND 
(
(
SELECT Sum([JONProjJoins].[Amount])
FROM [ProjOverview]
LEFT JOIN [JONProjJoins] ON [ProjOverview].[IDNmbr] = [JONProjJoins].[ProjID] 
GROUP BY [JONProjJoins].[ProjID] 
) <= 0
)
)

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36474452
or maybe you are looking for this
Update [ProjOverview] 
Set 
[Status] = 'Complete',
[Complete] = Now ,
[TotalServicesAct] = [TotalServicesEst]
Where (
([Status] = 'Canceled') AND 
([TotalCost] > 0) AND 
([ProjID] in (SELECT PrjID FROM [ProjOverview] LEFT JOIN [JONProjJoins] ON [ProjOverview].[IDNmbr] = [JONProjJoins].[ProjID] GROUP BY [JONProjJoins].[ProjID] having Sum([JONProjJoins].[Amount]) <= 0 ))
)

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KimD2
ID: 36474474
Thank you for your comment -- this is my first experience using the update query.  I've made the structure correction.   I am still receiving the syntax error.  I believe it has to do with the WHERE portion of the statement.  
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36474509
try last query...
0
 

Author Closing Comment

by:KimD2
ID: 36474531
Perfect!  Thank you!!
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36474537
Also, your inner Select
  a).  does not  have a closing paren
  b).  is an incomplete expression:  should probably be written AND EXISTS (Select ... )
  c).  needs to be correlated back to [ProjOverview] so that it returns a single value per row of [ProjOverview].  You can accomplish that with a WHERE clause:
    AND EXISTS (Select ... WHERE ...)

     or better yet, move that inner select from the WHERE Clause to the JOIN Clause

      ... JOIN (SELECT Sum[JONProjJoins].[Amount]) AS [SumOfAmount], [JONProjJoins].[ProjID] FROM [ProjOverview] GROUP BY [JONProjJoins].[ProjID] ) as X
 ON  X.proj_id = [ProjOverview].[IDNmbr]

      ...
      WHERE x.SumOfAmount >= 0

0
 
LVL 42

Expert Comment

by:dqmq
ID: 36474541
oops, too late.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question