Solved

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

Posted on 2011-03-24
10
286 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 29
SQL Server 2012 r2 - Sum totals 2 25
Whats wrong in this query - Select * from tableA,tableA 11 31
aggregate query? 20 46
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views 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 Access…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…

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