Solved

Ms Access - Update Query

Posted on 2011-02-24
11
326 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

760 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

19 Experts available now in Live!

Get 1:1 Help Now