Solved

SQL Update Query with Summed Field VB6 MS Access 2003

Posted on 2011-09-02
8
427 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:HainKurt
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:
HainKurt 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:HainKurt
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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Expert Comment

by:HainKurt
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now