Solved

SQL Update Query with Summed Field VB6 MS Access 2003

Posted on 2011-09-02
8
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 53

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 53

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 53

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 53

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

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 …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

738 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