?
Solved

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

Posted on 2011-03-24
10
Medium Priority
?
304 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
[X]
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
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

770 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