Recordset is not updatable (query from two tables)

喜夏-厌秋 提交于 2020-01-07 05:04:06

问题


I am using Microsoft Access 2013 with Sharepoint lists and I have two tables:

  • students: ID, Full name, Mobile, Start_Date (the date that they start studying at my centre), and some irrelevant fields

=> there are many students with different starting dates

  • [Weeks Off]: ID, Reason, From_Date (the date that the centre is temporarily closed), [Number of Weeks] (the number of weeks that the centre is temporariliy closed from that From_Date)

=> in each student' study time, they may have some "weeks off" that are not counted in the total number of weeks that they have studied for.

I am creating a query to calculate the number of weeks that students have studied from their Start_Date.

SELECT

students.ID,
students.[Full name],
students.Mobile,
students.Start_Date,
Round((Date()-students.[Start_Date])/7,0) - 
( SELECT SUM(
    IIF( [Weeks Off].[From Date]> students.[Start_Date] and [Weeks Off].[From Date]<Date(), 
         [Weeks Off].[Number of Weeks], 0 )
   ) 
  FROM [Weeks Off]
) AS [Studied Weeks],

FROM students;

The problem now is that even though the query successfully displays all students with a column showing their "Studied Weeks", the Recordset is not updatable.

How can I make it updatable again?


From comment:

I changed it into:

(Round( ( Date()- students.Start_Date)/7,0) - 
 DSum("[Number of Weeks]", "[Weeks Off]", 
      "[From Date]>= students.Start_Date And [From Date]<= Date()") 
) AS [Studied Weeks] 

But it says: Microsoft cannot find the name students.Start_Date you entered in the expression. So I'm still stuck here.


回答1:


See: Dealing with Non-Updateable Microsoft Access Queries and Allen Browne: Why is my query read-only?

From the latter:

It uses First(), Sum(), Max(), Count(), etc. in the SELECT clause. Queries that aggregate records are read-only.

It might work if you put the calculation into a separate query and join that (on Student.ID) to the Students table.

It will work (but may be slower) if you convert the SUM calculation into a DSum() expression. Then only that column will be read-only.

Edit

students.Start_Date is a variable in your DSum call, so it must be outside the constant string in the criteria.

Use Gustav's CSql() function to format the date and concatenate it with the rest.

(Round( ( Date()- students.Start_Date)/7,0) - 
 DSum("[Number of Weeks]", "[Weeks Off]", 
      "[From Date]>=" & CSql(students.Start_Date) & " And [From Date]<= Date()") 
) AS [Studied Weeks] 

should do it.



来源:https://stackoverflow.com/questions/45127060/recordset-is-not-updatable-query-from-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!