简体   繁体   中英

Recordset is not updatable (query from two tables)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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