?
Solved

Ms Access - Update Query

Posted on 2011-02-24
11
Medium Priority
?
332 Views
Last Modified: 2012-05-11
Hi,

I need help with an update query that I just can not get my head around right now.

I get data from our HR system to populate the table tblOpLogQry The data may contain the same employee multiple times of when they have been overseas.

I need to update the table/fields (tblPersDetailsOPR) with the data from tblOpLogQry using only the max date row where tblOpLogQry!ID = tblPersDetailsOPR!EID.

Fields to be updated:
tblPersDetailsOPR!OPR-LastDeployRTUDate UPDATE TO tblOpLogQry!RTADate
tblPersDetailsOPR!OPR-LastDeployRTU UPDATE TO tblOpStructure!OPCode

The second field above (OpCode), I would prefer this to be updated with the value from tblOpStructure!OpName where tblOpLogQry![Operation Code] = tblOpStructure!OpCode.

I hope all this makes sense. I have attached an example mdb with some fake data.

Thanks in advance to anyone who may help.

Kev









ExpertsExchange.mdb
0
Comment
Question by:Kev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34969152
if you want to learn how to write such updates, please read this article:
http://www.experts-exchange.com/A_1517.html

the syntax for ms access is a bit down in the user comments
happy coding
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34973581
Does this do it?

UPDATE tblPersDetails a
INNER JOIN tblOpLogQry b ON a.EID = b.ID
INNER JOIN tblOpStructure c ON b.[Operation Code] = c.OpCode SET
a.OPR-LastDeployRTUDate = b.RTADate,
a.OPR-LastDeployRTU = b.OPCode

0
 
LVL 5

Author Comment

by:Kev
ID: 34976779
Hi Grayl,

Getting error, "Syntax error (missing operator) in query expression....."

Kev
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 44

Expert Comment

by:GRayL
ID: 34981629
Could you post a screen shot of the error message window?  

You can see if the JOINs work by replacing 'UPDATE' with 'SELECT * FROM'.  In addition, as the field names contain a 'strange' character (-), it's probably wise to wrap them both in square brackets.

SELECT * FROM tblPersDetails a
INNER JOIN tblOpLogQry b ON a.EID = b.ID
INNER JOIN tblOpStructure c ON b.[Operation Code] = c.OpCode SET
a.[OPR-LastDeployRTUDate] = b.RTADate,
a.[OPR-LastDeployRTU] = b.OPCode;

0
 
LVL 5

Author Comment

by:Kev
ID: 34992686
Hi Grayl,

Error code attached.

Kev.
Error.png
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34992775
Lets start over:

UPDATE tblPersDetails a
INNER JOIN tblOpLogQry b
(INNER JOIN tblOpStructure c ON b.[Operation Code] = c.OpCode)
 ON a.EID = b.ID SET
a.OPR-LastDeployRTUDate = b.RTADate,
a.OPR-LastDeployRTU = b.OPCode;


0
 
LVL 5

Author Comment

by:Kev
ID: 34993492
Still getting another error, this time "Syntax error in update statement".

Have you tried the code in the example I uploaded ? If so, did it work ? I want to rule out me doing something wrong.

Kev
0
 
LVL 44

Accepted Solution

by:
GRayL earned 2000 total points
ID: 35003264
I had to back to the question and after a very thorough re-read, and 4 hours of testing I came up with this.  Although you have 96 records in the original table, only 90 will update because of mismatches/nulls:

UPDATE tblPersDetailsOPR e, (SELECT a.ID, c.OPName, a.RTADate
FROM tblOpLogQry AS a INNER JOIN tblOpStructure AS c ON a.[Operation Code] = c.OPCode
WHERE (((a.RTADate)=(SELECT Max(b.RTADate) FROM tblOpLogQry b WHERE b.ID = a.ID)))
ORDER BY a.ID) d SET
e.[OPR-LastDeployRTU] = d.OPName,
e.[OPR-LastDeployRTUDate] = d.RTADate
WHERE e.LinkEID = d.ID;
0
 
LVL 5

Author Comment

by:Kev
ID: 35003362
Wow, thank you very much.
Yep, there will not always be matches.
I tested it in my DB and it worked perfectly.
Thanks a lot for your continued assistance, I think I just learned a lot about update queries with subqueries.

Kev
0
 
LVL 5

Author Closing Comment

by:Kev
ID: 35003373
Thank you so much for an outstanding solution and a lot of work on your part.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35003391
Thanks, glad to help,

From an old Fighter Pilot
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

719 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