Solved

Ms Access - Update Query

Posted on 2011-02-24
11
328 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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
 
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

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.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

777 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