Solved

SQL Update Query with Summed Field VB6 MS Access 2003

Posted on 2011-09-02
8
430 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

895 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

11 Experts available now in Live!

Get 1:1 Help Now