Solved

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

Posted on 2011-03-24
10
279 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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:GRayL
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

15 Experts available now in Live!

Get 1:1 Help Now