Link to home
Start Free TrialLog in
Avatar of jtovar3
jtovar3Flag for United States of America

asked on

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

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.
Avatar of jtovar3
jtovar3
Flag of United States of America image

ASKER

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"
Avatar of Nick67
"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
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";
Avatar of jtovar3

ASKER

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.
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.
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.  
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.
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?
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
ASKER CERTIFIED SOLUTION
Avatar of Nick67
Nick67
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial