Solved

Ms Access - Update Query

Posted on 2011-02-24
11
327 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:budorat
  • 5
  • 5
11 Comments
 
LVL 142

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:budorat
ID: 34976779
Hi Grayl,

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

Kev
0
 
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:budorat
ID: 34992686
Hi Grayl,

Error code attached.

Kev.
Error.png
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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:budorat
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 500 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:budorat
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:budorat
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now