Current location: Hot Scripts Forums » Other Discussions » Database » Update based on select (MS Access)


Update based on select (MS Access)

Reply
  #1 (permalink)  
Old 06-28-07, 02:31 AM
odi's Avatar
odi odi is offline
Newbie Coder
 
Join Date: Oct 2006
Location: Switzerland
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Update based on select (MS Access)

I have the following problem:
I must update a field in a table in an Access database. The value is saved in another table, where I have to count records grouped by an ID.
My solution is to run a query that does the counting and wrap this in an update statment:

PHP Code:

Dim SQLString As String


SQLString 
"UPDATE tblClient "
SQLString SQLString "SET total = subclient.counter "
SQLString SQLString "FROM "
SQLString SQLString "( "
SQLString SQLString "      SELECT FK_Client, COUNT(FK_Client)  counter "
SQLString SQLString "      FROM tblSubClient GROUP BY FK_Client"
SQLString SQLString ") subclient "
SQLString SQLString "WHERE tblClient.ClientID = subclient.FK_Client"

DoCmd.RunSQL (SQLString
Something seems to be wrong, because I always get an error message saying "missing operator" (from MS Access).

Now I don't know if it's a general SQL syntax problem or is it because of the table alias (?)

Any idea?
Reply With Quote
  #2 (permalink)  
Old 06-28-07, 04:27 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
i think your query should be something like this:
SQL Code:
  1. UPDATE
  2.   tblClient
  3. SET
  4.   total = (
  5.     SELECT
  6.       COUNT(FK_Client)
  7.     FROM
  8.       tblSubClient
  9.     WHERE
  10.       subclient.FK_Client = tblClient.ClientID
  11.   )
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 06-29-07, 06:33 PM
odi's Avatar
odi odi is offline
Newbie Coder
 
Join Date: Oct 2006
Location: Switzerland
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
I figured out that this is not possible with MS Access, because it does not allow updates based on aggregate functions (such as count(),sum(),min(),max() etc.).

I did the most ugly thing that came up my mind:

I loop though my customer ids and update each records with a single statement. But there are only about 30 customers, so it shouldn't be that bad.
Reply With Quote
  #4 (permalink)  
Old 10-22-09, 04:29 PM
ted_250 ted_250 is offline
New Member
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb

Quote:
Originally Posted by odi View Post
I figured out that this is not possible with MS Access, because it does not allow updates based on aggregate functions (such as count(),sum(),min(),max() etc.).

I did the most ugly thing that came up my mind:

I loop though my customer ids and update each records with a single statement. But there are only about 30 customers, so it shouldn't be that bad.
I've seen similar updates that used aggregates; they used an unusual syntax similar to:

UPDATE table1 AS T1
INNER JOIN Table2 AS T2 ON T1.PK = T2.PK
SET T1.Col = T2.Col

Ted
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ms Access amjad_system Database 3 05-25-07 08:13 PM
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 01:55 AM
change options of select based on selection in other select nassau JavaScript 3 08-31-06 08:00 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 05:01 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.