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
Solved

SQL Update Query with Summed Field VB6 MS Access 2003

Posted on 2011-09-02
8
436 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

789 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