简体   繁体   English

记录集不可更新(来自两个表的查询)

[英]Recordset is not updatable (query from two tables)

I am using Microsoft Access 2013 with Sharepoint lists and I have two tables: 我将Microsoft Access 2013与Sharepoint列表一起使用,并且有两个表:

  • students : ID, Full name, Mobile, Start_Date (the date that they start studying at my centre), and some irrelevant fields 学生 :ID,全名,手机,Start_Date(他们在我中心开始学习的日期)以及一些无关的字段

=> 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) [Weeks Off] :ID,原因,From_Date(中心暂时关闭的日期),[周数](中心从该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 . 我正在创建一个查询,以计算学生从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. 但它说:Microsoft找不到学生的名字。您在表达式中输入的起始日期。 So I'm still stuck here. 所以我仍然停留在这里。

See: Dealing with Non-Updateable Microsoft Access Queries and Allen Browne: Why is my query read-only? 请参阅: 处理不可更新的Microsoft Access查询艾伦·布朗Allen Browne):为什么查询是只读的?

From the latter: 从后者:

It uses First(), Sum(), Max(), Count(), etc. in the SELECT clause. 它在SELECT子句中使用First(),Sum(),Max(),Count()等。 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. 如果将计算结果放到一个单独的查询中(并将该查询(在Student.ID上)加入“学生”表,则可能会起作用。

It will work (but may be slower) if you convert the SUM calculation into a DSum() expression. 如果将SUM计算转换为DSum()表达式,它将起作用(但可能会更慢)。 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. students.Start_DateDSum调用中的变量,因此它必须在条件中的常量字符串之外。

Use Gustav's CSql() function to format the date and concatenate it with the rest. 使用Gustav的CSql()函数格式化日期并将其与其余日期连接在一起。

(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. 应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM