Liliane said:
Hi,
I have two tables: tbl_Audits and tbl_AAL_Items. Now I need to update
AAL_Completed field in tbl_Audit with latest date select from tbl_AAL_Items.
I wrote a query but it's not working.
UPDATE tbl_Audits INNER JOIN tbl_AAL_Items ON tbl_Audits.Audit_No =
tbl_AAL_Items.Audit_No
SET tbl_Audits.AAL_Completed = Max([date_Actioned])
WHERE (((tbl_Audits.No_of_Items)=[completed]) AND
((tbl_Audits.Completed)<>0));
How can I get the latest action date and update AAL_complete?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
You can't use MAX() like that. Try the following (I use a derived
table: the SELECT command in parentheses) [probably won't work, but if
it does it should be the faster query]:
UPDATE tbl_Audits INNER JOIN (SELECT Audit_No, MAX(date_Actioned) As
LatestDate FROM tbl_AAL_Items GROUP BY Audit_No) As AC ON
tbl_Audits.audit_no = AC.audit_no
SET tbl_Audits.AAL_Completed = AC.LatestDate
WHERE tbl_Audits.No_of_Items = [completed] AND tbl_Audits.Completed <> 0
If that doesn't work try the Microsoft recommended solution (use a
domain aggregaate function), which may take a while to run, depending on
the size of the tables.
UPDATE tbl_Audits
SET AAL_Completed = DMax("date_Actioned", "tbl_AAL_Items", "Audit_No=" &
tbl_Audits.Audit_No)
WHERE No_of_Items = [completed] AND Completed <> 0
This WHERE clause is screwy 'cuz [completed] needs to be referenced - is
it a column in tbl_AAL_Items or a user-defined parameter or are you
referring to the column in tbl_Audits? If it is a user-defined
parameter you need to name it something different and use the PARAMETERS
clause. Like this:
PARAMETERS completed_items INTEGER;
UPDATE...
SET ... etc. ...
WHERE No_of_Items = completed_items AND Completed <> 0
If it is in tbl_AAL_Items then the expression needs to go in the DMax()
function's criteria parameter. Like this:
"Audit_No=" & tbl_Audits.Audit_No & " AND [completed] = " &
tbl_Audits.No_of_Items)
and removed from the main query's WHERE clause.
WHERE Completed <> 0
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
** Respond only to this newsgroup. I DO NOT respond to emails **
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBSaTBdoechKqOuFEgEQLe8QCfRuunoN9of97j6X8eyVl5txWP0kMAn2hq
lDwisEzkATSM0x3nIU97imRC
=7nNa
-----END PGP SIGNATURE-----