Solved

how can i update a table based on the result of a count query?

Posted on 2011-03-24
10
283 Views
Last Modified: 2012-08-13
This is probably an easy fix but I am trying to update a table with a count query that I had created. When I run the count query, it produces a result fine, but when i try to put it into an update query, it fails. I am sure I am missing how I associate

UPDATE tbl_Metrics 
SET Result = (SELECT Count(tbl_Observation.Obs_ID) AS CountOfObs_ID
FROM tbl_Audit INNER JOIN tbl_Observation ON tbl_Audit.Audit_ID=tbl_Observation.Audit_ID
WHERE (((tbl_Observation.Due_Date)>DateAdd('d',-30,Date())) AND ((tbl_Observation.Status)='In Progress')))
WHERE tbl_Metics.[Metric-Name] = "Count_30DaysPast";

Open in new window


I would like to update my table : Metrics, Column name: Result, for the record "Count_30DaysPast" in the [Metric-Name] column.
0
Comment
Question by:jtovar3
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:jtovar3
ID: 35208735
no that is fine with me, i appreciate your efforts. whether single or double quotes, i still get an error

"Operation must use an updateable query"
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35209567
"Operation must use an updateable query"

Open the table/query you are trying to update.
Can you manually change a value?

You have to be able to do that FIRST.
Select Distinct, and some other types of multi-table queries are NOT updateable.
If Results is a calculated field, it may not be updateable.

Get a Select query that you can update working first, then see about changing it to an Update query
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35209671
Somehow the parentheses don't look right - and I changed to all double quotes.  Try this:

UPDATE tbl_Metrics
SET Result = (SELECT Count(tbl_Observation.Obs_ID)
FROM tbl_Audit INNER JOIN tbl_Observation ON tbl_Audit.Audit_ID=tbl_Observation.Audit_ID
WHERE tbl_Observation.Due_Date > DateAdd("d",-30,Date() AND tbl_Observation.Status="In Progress")
WHERE tbl_Metics.[Metric-Name] = "Count_30DaysPast";
0
 

Author Comment

by:jtovar3
ID: 35209700
What do you mean get a Select Query that you can update working first?

My select query does select and count the number of records. I just want to take that number and update this separate table with that number.

I'm trying to create a Metrics table.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35209776
You are selecting something from that separate table
Create a query
"Select Result from tbl_Metrics;"

Now, can you actually CHANGE that value in a simple select query?
What
"Operation must use an updateable query"
is trying to tell you is that tbl_Metrics.Result is not something that you can change, at least not in the way you've presently written the query.

By <Get a Select query that you can update working first> I mean try to manually change the value of tbl_Metrics.Result.
Can you?

If you change your update query back to a select query, can you change the value of Results when you display the values.
That is the first place to start with the error you posted.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 44

Expert Comment

by:GRayL
ID: 35209899
As the Aggregate function in a query produces a result that is not the name of a field in either table, Jet considers the sub query to be not updatable.  If you were to try and use DCount()  and put the Join in the table parameter, it too would fail as a domain aggregate function will only accept the name of a singe table or stored query.  In other words, you can't get there from here.  
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35220204
Hi Nick67:  In case you are missing my drift, what you are trying to do in Access cannot be done - at least from my testing and experience.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35224901
This may be incredibly dumb.
Line 5 has a spelling error

tbl_Metics not tbl_Metrics

UPDATE tbl_Metrics
SET Result = (SELECT Count(tbl_Observation.Obs_ID) AS CountOfObs_ID
FROM tbl_Audit INNER JOIN tbl_Observation ON tbl_Audit.Audit_ID=tbl_Observation.Audit_ID
WHERE (((tbl_Observation.Due_Date)>DateAdd('d',-30,Date())) AND ((tbl_Observation.Status)='In Progress')))
WHERE tbl_Metics.[Metric-Name] = "Count_30DaysPast";

Now,  darn it, even if I need to use a wrapper function in VBA -- we'll get this to work.
Can you post a sample?
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35225003
It's late at night now.
I have a proof-of-concept that a wrapper function will work
I am trying to adapt it for you.
I have a problem
I don't see how your Result relates to tbl_Metrics.[Metric-Name]

Basically your query was

UPDATE tbl_Metrics
SET Result = (SELECT Count(tbl_Observation.Obs_ID)
FROM tbl_Audit INNER JOIN tbl_Observation ON tbl_Audit.Audit_ID=tbl_Observation.Audit_ID
WHERE tbl_Observation.Due_Date > DateAdd("d",-30,Date() AND tbl_Observation.Status="In Progress")
WHERE tbl_Metics.[Metric-Name] = "Count_30DaysPast";

You take out the count query and replace it with MyResult([tbl_Metrics.[Metric-Name]])

UPDATE tbl_Metrics
SET Result = MyResult([tbl_Metrics.[Metric-Name]])
WHERE tbl_Metrics.[Metric-Name] = "Count_30DaysPast";


Now, in a module, you create a public function
Here is most of it ***BUT***
I don't see how your Result relates to tbl_Metrics.[Metric-Name] and that needs to be ironed out before this will work

'______________________________
Option Compare Database
Option Explicit

Public Function MyResults(JobCode As String)

Dim db As Database
Dim rs As Recordset
Dim SQL as string

SQL = "SELECT Count(tbl_Observation.Obs_ID) AS CountOfObs_ID FROM tbl_Audit INNER JOIN tbl_Observation ON tbl_Audit.Audit_ID=tbl_Observation.Audit_ID WHERE (((tbl_Observation.Due_Date)>DateAdd('d',-30,Date())) AND ((tbl_Observation.Status)='In Progress')))"


Set db = CurrentDb
Set rs = db.OpenRecordset(SQL, dbOpenDynaset, dbSeeChanges)
MyResults = rs!CountOfJobCode

End Function
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 35229040
Perhaps they aren't related.
Does your Count only return a single value, regardless of WHERE tbl_Metrics.[Metric-Name] = "Count_30DaysPast"?
In which case, the query becomes

UPDATE tbl_Metrics
SET Result = MyResults()
WHERE tbl_Metrics.[Metric-Name] = "Count_30DaysPast";

and the function/module becomes

Option Compare Database
Option Explicit

Public Function MyResults(JobCode As String)

Dim db As Database
Dim rs As Recordset
Dim SQL as string

SQL = "SELECT Count(tbl_Observation.Obs_ID) AS CountOfObs_ID FROM tbl_Audit INNER JOIN tbl_Observation ON tbl_Audit.Audit_ID=tbl_Observation.Audit_ID WHERE (((tbl_Observation.Due_Date)>DateAdd('d',-30,Date())) AND ((tbl_Observation.Status)='In Progress')))"


Set db = CurrentDb
Set rs = db.OpenRecordset(SQL, dbOpenDynaset, dbSeeChanges)
MyResults = rs!CountOfJobCode

End Function

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

948 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

21 Experts available now in Live!

Get 1:1 Help Now